Version 4 of Rust

Updated 2022-05-27 21:22:04 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.

Like Tcl, Rust does not employ a garbage collector, but instead assigns each value an owning block of code, and deallocates resources for the value when the program exits that block of code. Assigning the value to a new variable or passing it to another function moves the ownership to that variable or function, deallocating the current variable to prevent aliasing. As a result, a variable may not be valid for the entire duration of a block of code.

Like C, & produces a reference to a value. Taking a reference is referred to as borrowing because the compiler tracks each reference, ensures that it does not outlive its referent, and additionally ensures that at each moment there is only one mutable reference. This avoids a whole class of memory errors that C programs are susceptible to. * follows a reference to access the value.

A variable is immutable by default, meaning that it does not afford write access to the data bound to it. If a variable is annotated with mut, the variable may then be used to mutate the bound value. The value bound to a mutable variable may be borrowed, but the compiler ensures that it is never borrowed more than once at any given point during execution. This eliminates data races in Rust programs.

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 sructures, 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.

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.

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.