Version 11 of Rust

Updated 2022-06-14 15:20:35 by pooryorick

Rust is a programming language designed to be performant, safe, and productive.

Description

Rust combines concepts from C, C++, Haskell, and other languages. In order to guarantee type safety and compile a program to performant machine code, the compiler tracks the type of each variable and function. Where the compiler can not infer a type of a variable, parameter, or result, it must be annotated with a type. In order to guarantee memory safety, the compiler also tracks the lifetime of each variable. Where the compiler can not infer the lifetime of a variable, parameter or result, it must be annotated with a lifetime.

Rust code is organized into blocks. Like Tcl, Rust does not employ a garbage collector, but it also does not normally employ reference counting. Instead each new variable has a scope, and if a variable holds an allocated value when it goes out of scope, that value is deallocated. When a value is passed to a function, is is considered moved, and is no-longer accessible in the original scope, which prevents aliasing. This means that the sope of a variable may not extend to the end of the block. & May be used to pass a reference to a value rather than passing the value itself, in which case the original scope continues to own the value, and the borrowing scope must complete before the original scope does so that the reference does not outlive the thing it refers to. This managment of data lifetimes eliminates a whole class of issues that a C program might be vulnerable to.

A variable is immutable by default. mut declares a variable to be mutable, and &mut declares a reference to be mutable. Only one mutable reference may exist at any given point. This eliminates data races.

A program is composed primarily of structs and the functions that operate on them. A struct in turn is composed of primitive values, structs, enums, and references to any of those things or to functions. An enum is a type that enumerates any number of possible variants, each of which may be struct. A function that accepts an enum uses match to tailor its operation to each possible variant of the enum.

Programs and libraries are organized into modules, which contain any of the various programming artifacts Rust provides, namely structures, variables, functions, traits, and trait implementations.

A trait is a specification for group of functions that act as an interface to a struct. Any number of traits may be implemented for a given struct. impl is used to provide a set of function definitions that implement the trait for a particular type of struct. To maintain coherence, a module may only implement a trait if either the trait or the structure is local to the module.

Some traits have special meaning for Rust. A struct has the copy trait is copied when it would otherwise have been moved, meaning that both the original scope and the new scope each own an independent copy of the value.

Rust has various features for programming the structure of the program itself, i.e. for metaprogramming: A macro language, closures, syntax for templating generic structs and functions, and pattern matching similar to that of Haskell, on language items for such purposes as destructuring variable assignment and code branching according to data type.

Rust is actually segmented into two different languages: safe Rust and unsafe Rust. In the safe parts, nothing is undefined, nothing is null, only safe type conversions are possible, and only safe memory operations are allowed. There is, however, an escape hatch: A function annotated as unsafe takes upon itself the responsibility to guarantee the same things that the Rust compiler guarantees. , so the compiler makes no further effort to check that function for conformance. This makes it possible to write such a function in another language such as C, if needed.

One of the design goals of Rust is for the compiler to be able, as much as possible to infer the intent of the programmer. The compiler also takes a more active role than most compilers in making suggestions for fixing problems that it finds. This helps to smooth the learning curve.

Memory Management: Tcl Vs Rust

Both Tcl and Rust provide automatic memory management without using a garbage collector, and both are memory-safe. Where Tcl uses reference counting, Rust uses its borrow rules to constrain the use of references, and tracks all references at compile time to ensure that a value may be cleaned up immediately when the program exits from the block that owns it. Tcl's internal reference counting relies on the developer to properly increment and decrement references, which is error-prone since proper reference counting can not be checked for correctness. Because Rust would automate the task of reference counting, relieving the developers of Tcl from this burden, it may prove to be an ideal implementation language for Tcl.

Object Orientation

Rust does not provide traditional object-oriented programming' facilities. Inheritance has proven to be a liability, so Rust doesnt have it. Instead, the emphasis is on structures and interfaces that those structures support. Each structure has a type, and where inheritance might be used in other languages, it is common instead to create a new type of structure and implement customized traits for it. Composition is achieved by having a field in one structure be a reference to another structure. This design allows for zero-cost abstractions, since all the details of function dispatch are worked out at compile time. Where needed, Rust also provides facilities that provide for runtime dispatch.

Rust Vs. Tcl

Both languages are founded on the concept of immutable value, but Tcl values are much more abstract, both semantically and technically. In Tcl, copy-on-write can be implemented underneath the surface of the language whereas in Rust, a programmer details directly with such issues. This level of abstraction is the most fundamental difference between the languages.

As value-oriented languages, both languages have no concept of null, which is anything but a value.

Both languages are memory-safe, assuming that the underlying implementation/compiler is free of bugs. Rust is type-safe, both in the sense of memory layout and data structure, and also in the sense of the semantics of the data and the operations on it, as only the intended structure is accepted as the argument of a function or the value of a variable. while Tcl is not type-safe, it is also not type-unsafe: Each procedure may choose to implement the necessary runtime type verification. However, even then a procedure has no way to determine which program component the declared the data type, and therefore can never achieve the level of semantic type safety that Rust does.

For a program written in Rust, the additional burden of annotating data types and tracking data mutability and ownership is high compared to practice of taking things at face value in Tcl, and all the type annotation, templating, and boilerplate code makes a program written in Rust far more verbose, and development far slower. Tcl is far more concise than Rust, but far less performant. In addition to performance, another big win for Rust is that all the additional type information a programmer provides in the source code allows the compiler to verify some aspects of its correctness, and probably reduces the size any accompanying test suite by at least the same amount of code and effort. If a program compiles successfully, it's already some indication that the program works as expected. An equivalent program in Tcl would have to ensure the same level of correctness via a series of unit tests.


"When you use Rust, it is sometimes outright preposterous how much knowledge of language, and how much of programming ingenuity and curiosity you need in order to accomplish the most trivial things. When you feel particularly desperate, you go to rust/issues and search for a solution for your problem. Suddenly, you find an issue with an explanation that it is theoretically impossible to design your API in this way, owing to some subtle language bug. The issue is Open and dated Apr 5, 2017." [L1 ]