Version 0 of Rust

Updated 2022-05-08 17:31:19 by pooryorick

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


Rust combines concepts from C, C++, and Haskell. In order to guarantee type safety and compile 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.

Like Tcl, Rust does not employ a garbage collector, but tracks a value through its lifetime and deallocates the resources for it at the end of its lifetime. For each value, there is always exactly one variable that owns it. Like C, the & operator produces a reference to the value held in a variable. In Rust this is called borrowing because the compiler does not allow the lifetime of a reference to be longer than the lifetime of the the owning variable. This avoids a whole class of memory errors that C programs are susceptible to. The * operator either produces the referenced value from a reference, or allows for mutating the referenced value.

The value in each variable is immutable by default. In order to mutate a value, the variable that owns it must be annotated with the mut keyword. Borrowing a mutable value is a allowed, but the compiler ensures that it is never borrowed more than once at any given point. This eliminates data races in Rust programs.

A program is composed primarily of structs and the functions that operate on them. A struct is in turn 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.

A group of functions declared as an interface called a trait. For each type of struct which supports a trait, the imp keyword is used to provide a set of function definitions that implement the trait for that type of struct.

Rust features 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. When a function is annotated as unsafe it means that the function takes the responsibility for all of Rust's safety guarantees on itself, and 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.