Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others.[18][19] The designers refined the language while writing the Servo experimental browser engine,[20] and the Rust compiler. It has gained increasing use in industry, and Microsoft has been experimenting with the language for secure and safety-critical software components.[21][22]
Rust has been voted the "most loved programming language" in the Stack Overflow Developer Survey every year since 2016, though only used by 7% of the respondents in the 2021 survey.[23]
History
An example of compiling a Rust program
The language grew out of a personal project begun in 2006 by Mozilla employee Graydon Hoare,[13] who stated that the project was possibly named after the rust family of fungi.[24] Mozilla began sponsoring the project in 2009[13] and announced it in 2010.[25][26] The same year, work shifted from the initial compiler (written in OCaml) to the LLVM-based self-hosting compiler written in Rust.[27] Named rustc, it successfully compiled itself in 2011.[28]
The first numbered pre-alpha release of the Rust compiler occurred in January 2012.[29] Rust 1.0, the first stable release, was released on May 15, 2015.[30][31] Following 1.0, stable point releases are delivered every six weeks, while features are developed in nightly Rust with daily releases, then tested with beta releases that last six weeks.[32][33] Every 2 to 3 years, a new Rust "Edition" is produced. This is to provide a easy reference point for changes due to the frequent nature of Rust's Train release schedule, as well as to provide a window to make breaking changes. Editions are largely compatible.[34]
Along with conventional static typing, before version 0.4, Rust also supported typestates. The typestate system modeled assertions before and after program statements, through use of a special check statement. Discrepancies could be discovered at compile time, rather than at runtime, as might be the case with assertions in C or C++ code. The typestate concept was not unique to Rust, as it was first introduced in the language NIL.[35] Typestates were removed because in practice they were little used,[36] though the same functionality can be achieved by leveraging Rust's move semantics.[37]
The object system style changed considerably within versions 0.2, 0.3, and 0.4 of Rust. Version 0.2 introduced classes for the first time, and version 0.3 added several features, including destructors and polymorphism through the use of interfaces. In Rust 0.4, traits were added as a means to provide inheritance; interfaces were unified with traits and removed as a separate feature. Classes were also removed and replaced by a combination of implementations and structured types.[citation needed]
Starting in Rust 0.9 and ending in Rust 0.11, Rust had two built-in pointer types: ~ and @, simplifying the core memory model. It reimplemented those pointer types in the standard library as Box and (the now removed) Gc.
In January 2014, before the first stable release, Rust 1.0, the editor-in-chief of Dr. Dobb's, Andrew Binstock, commented on Rust's chances of becoming a competitor to C++ and to the other up-and-coming languages D, Go, and Nim (then Nimrod). According to Binstock, while Rust was "widely viewed as a remarkably elegant language", adoption slowed because it repeatedly changed between versions.[38]
Rust has a foreign function interface (FFI) that can be called from, e.g., C language, and can call C. While calling C++ has historically been problematic (from any language), Rust has a library, CXX, to allow calling to or from C++, and "CXX has zero or negligible overhead."[39]
In August 2020, Mozilla laid off 250 of its 1,000 employees worldwide as part of a corporate restructuring caused by the long-term impact of the COVID-19 pandemic.[40][41] Among those laid off were most of the Rust team,[42][better source needed] while the Servo team was completely disbanded.[43][better source needed] The event raised concerns about the future of Rust.[44]
In the following week, the Rust Core Team acknowledged the severe impact of the layoffs and announced that plans for a Rust foundation were underway. The first goal of the foundation would be taking ownership of all trademarks and domain names, and also take financial responsibility for their costs.[45]
The syntax of Rust is similar to C and C++, with blocks of code delimited by curly brackets, and control flow keywords such as if, else, while, and for, although the specific syntax for defining functions is more similar to Pascal. Despite the resemblance to C and C++, the syntax of Rust in is closer to that of the ML family of languages and the Haskell language. Nearly every part of a function body is an expression,[49] even control flow operators. For example, the ordinary if expression also takes the place of C's ternary conditional, an idiom used by ALGOL 60. As in Lisp, a function need not end with a return expression: in this case if the semicolon is omitted, the last expression in the function creates the return value, as seen in the following recursive implementation of the factorial function:
The following iterative implementation uses the ..= operator to create an inclusive range:
fnfactorial(i: u64)-> u64{(2..=i).product()}
Features
A presentation on Rust by Emily Dunham from Mozilla's Rust team (linux.conf.au conference, Hobart, 2017)
Rust is intended to be a language for highly concurrent and highly safe systems,[50] and programming in the large, that is, creating and maintaining boundaries that preserve large-system integrity.[51] This has led to a feature set with an emphasis on safety, control of memory layout, and concurrency.
Memory safety
Rust is designed to be memory safe. It does not permit null pointers, dangling pointers, or data races.[52][53][54] Data values can be initialized only through a fixed set of forms, all of which require their inputs to be already initialized.[55] To replicate pointers being either valid or NULL, such as in linked list or binary treedata structures, the Rust core library provides an option type, which can be used to test whether a pointer has Some value or None.[53] Rust has added syntax to manage lifetimes, which are checked at compile time by the borrow checker. Unsafe code can subvert some of these restrictions using the unsafe keyword.[15]
There is the concept of references (using the & symbol), which does not involve run-time reference counting. The safety of such pointers is verified at compile time, preventing dangling pointers and other forms of undefined behavior. Rust's type system separates shared, immutable pointers of the form &T from unique, mutable pointers of the form &mut T. A mutable pointer can be coerced to an immutable pointer, but not vice versa.
Ownership
Rust has an ownership system where all values have a unique owner, and the scope of the value is the same as the scope of the owner.[57][58] Values can be passed by immutable reference, using &T, by mutable reference, using &mut T, or by value, using T. At all times, there can either be multiple immutable references or one mutable reference (an implicit readers–writer lock). The Rust compiler enforces these rules at compile time and also checks that all references are valid.
Rust uses type inference for variables declared with the keywordlet. Such variables do not require a value to be initially assigned to determine their type. A compile time error results if any branch of code leaves the variable without an assignment.[59] Variables assigned multiple times must be marked with the keyword mut (short for mutable).
Functions can be given genericparameters, which usually constrain the generic type to implement a particular trait or traits. Within such a function, the generic value can only be used through those traits. This means that a generic function can be type-checked as soon as it is defined.
The implementation of Rust generics is similar to the typical implementation of C++ templates: a separate copy of the code is generated for each instantiation. This is called monomorphization and contrasts with the type erasure scheme typically used in Java and Haskell. Rust's type erasure is also available by using the keyword dyn. The benefit of monomorphization is optimized code for each specific use case; the drawback is increased compile time and size of the resulting binaries.
In Rust user defined types are created with the struct<\code> keyword. These types usually contains fields of data like objects or classes in other languages. The impl keyword can define methods for the struct (data and function are defined separately in a struct) or implement a trait for the structure. A trait is a contract that a structure has certain required methods implemented. Traits are used to restrict generic parameters and because traits can provide a struct with more methods than the user defined. For example, the trait Iterator requires that the next method be defined for the type. Once the next method is defined the trait provides common functional helper methods over the iterator like map or filter.
The object system within Rust is based around implementations, traits and structured types. Implementations fulfill a role similar to that of classes within other languages and are defined with the keyword impl. Traits provide inheritance and polymorphism; they allow methods to be defined and mixed in to implementations. Structured types are used to define fields. Implementations and traits cannot define fields themselves, and only traits can provide inheritance. Among other benefits, this prevents the diamond problem of multiple inheritance, as in C++. In other words, Rust supports interface inheritance but replaces implementation inheritance with composition; see composition over inheritance.
Components
Rust features a large number of components that extend the Rust feature set and make Rust development easier. Component installation is typically managed by rustup, a Rust toolchain installer developed by the Rust project.[60]
Cargo
Cargo is Rust's build system and package manager. Cargo handles downloading dependencies, and building dependencies. Cargo also acts as a wrapper for clippy and other Rust components. It requires projects to follow a certain directory structure.[61]
The dependencies for a Rust package are specified in a Cargo.toml file along with version requirements, telling Cargo which versions of the dependency are compatible with the package. By default, Cargo sources its dependencies from the user-contributed registry crates.io but Git repositories and packages in the local filesystem can be specified as dependencies, too.[62]
Rustfmt
Rustfmt is a code formatter for Rust. It takes Rust source code as input and changes the whitespace and indentation to produce formatted code in accordance to the Rust style guide or rules specified in a rustfmt.toml file. Rustfmt can be invoked as a standalone program or on a Rust project through Cargo. [63][64]
Clippy
Clippy is Rust's built in linting tool to improve the correctness, performance, and readability of Rust code. As of 2021, Clippy has more than 450 rules,[65] which can be browsed online and filtered by category.[66] Some rules are disabled by default.
RLS
RLS is a language server that provides IDEs and text editors with more information about a Rust project. It provides linting checks via Clippy, formatting via Rustfmt, automatic code completion via Racer, among other functions.[67] Development of Racer was slowed down in favor of rust-analyzer.[68]
Language extensions
It is possible to extend the Rust language using the procedural macro mechanism.[69]
Procedural macros use Rust functions that run at compile time to modify the compiler's token stream. This complements the user-defined macro mechanism, which uses pattern matching to achieve similar goals.
Procedural macros come in three flavors:
Function-like macros custom!(...)
Derive macros #[derive(CustomDerive)]
Attribute macros #[CustomAttribute]
The println! macro is an example of a function-like macro and serde_derive[70] is a commonly used library for generating code
for reading and writing data in many formats such as JSON. Attribute macros are commonly used for language bindings such as the extendr library for Rust bindings to R.[71]
The following code shows the use of the Serialize, Deserialize and Debug derive procedural macros
to implement JSON reading and writing as well as the ability to format a structure for debugging.
Rust aims "to be as efficient and portable as idiomatic C++, without sacrificing safety".[72] Since Rust utilizes LLVM, any performance improvements in LLVM also carry over to Rust.[73]
The Rust Foundation is a non-profitmembership organization incorporated in Delaware, United States, with the primary purposes of supporting the maintenance and development of the language, cultivating the Rust project team members and user communities, managing the technical infrastructure underlying the development of Rust, and managing and stewarding the Rust trademark.
It was established on February 8, 2021, with five founding corporate members (Amazon Web Services, Huawei, Google, Microsoft, and Mozilla).[97]
The foundation's board is chaired by Shane Miller.[98] Its interim Executive Director is Ashley Williams.
^Hoare, Graydon (December 28, 2016). "Rust is mostly safety". Graydon2. Dreamwidth Studios. Archived from the original on May 2, 2019. Retrieved May 13, 2019.
^"Future Tense". April 29, 2011. Archived from the original on September 18, 2012. Retrieved February 6, 2012.
^Hoare, Graydon (July 7, 2010). Project Servo(PDF). Mozilla Annual Summit 2010. Whistler, Canada. Archived(PDF) from the original on July 11, 2017. Retrieved February 22, 2017.
^Hoare, Graydon (October 2, 2010). "Rust Progress". Archived from the original on August 15, 2014. Retrieved October 30, 2010.
^Klabnik, Steve; Nichols, Carol (June 2018). "Chapter 4: Understanding Ownership". The Rust Programming Language. San Francisco, California: No Starch Press. p. 44. ISBN978-1-593-27828-1. Archived from the original on May 3, 2019. Retrieved May 14, 2019.