Rust (programming language)

Rust is a general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety—meaning that all references point to valid memory—without a garbage collector. To simultaneously enforce memory safety and prevent data races, its "borrow checker" tracks the object lifetime of all references in a program during compilation.

Rust was influenced by ideas from functional programming, including immutability, higher-order functions, and algebraic data types. It is popular for systems programming. Rust does not enforce a programming paradigm, but supports object oriented programming with its structs, enums, traits and methods. Functional programming is supported via immutability, pure functions, higher order functions and pattern matching.

Software developer Graydon Hoare created Rust as a personal project while working at Mozilla Research in 2006. Mozilla officially sponsored the project in 2009. In the years following the first stable release in May 2015, Rust was adopted by companies including Amazon, Discord, Dropbox, Google (Alphabet), Meta, and Microsoft. In December 2022, it became the first language other than C and assembly to be supported in the development of the Linux kernel.

Rust has been noted for its rapid adoption, and has been studied in programming language theory research.

Origins (2006–2012)
Rust began as a personal project in 2006 by Mozilla Research employee Graydon Hoare, named after the group of fungi that are "over-engineered for survival". Mozilla began sponsoring the project in 2009, and would employ a dozen engineers to work on it full time over the next ten years.

Around 2010, work shifted from the initial compiler written in OCaml to a self-hosting compiler based on LLVM written in Rust. The new Rust compiler successfully compiled itself in 2011. In the fall of 2011, the Rust logo was developed based on a bicycle chainring.

Evolution (2012–2020)
Rust's type system underwent significant changes between versions 0.2, 0.3, and 0.4. In version 0.2, which was released in March 2012, classes were introduced for the first time. Four months later, version 0.3 added destructors and polymorphism, through the use of interfaces. In October 2012, version 0.4 was released, which added traits as a means of inheritance. Interfaces were combined with traits and removed as a separate feature; and classes were replaced by a combination of implementations and structured types.

Through the early 2010s, memory management through the ownership system was gradually consolidated to prevent memory bugs. By 2013, Rust's garbage collector was removed, with the ownership rules in place.

In January 2014, the editor-in-chief of Dr. Dobb's Journal, Andrew Binstock, commented on Rust's chances of becoming a competitor to C++, along with D, Go, and Nim (then Nimrod). According to Binstock, while Rust was "widely viewed as a remarkably elegant language", adoption slowed because it radically changed from version to version. The first stable release, Rust 1.0, was released on May 15, 2015.

The development of the Servo browser engine continued alongside Rust's own growth. In September 2017, Firefox 57 was released as the first version that incorporated components from Servo, in a project named "Firefox Quantum".

Mozilla layoffs and Rust Foundation (2020–present)
In August 2020, Mozilla laid off 250 of its 1,000 employees worldwide, as part of a corporate restructuring caused by the COVID-19 pandemic. The team behind Servo was disbanded. The event raised concerns about the future of Rust, as some members of the team were active contributors to Rust. 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 to take ownership of all trademarks and domain names, and take financial responsibility for their costs.

On February 8, 2021, the formation of the Rust Foundation was announced by its five founding companies (AWS, Huawei, Google, Microsoft, and Mozilla). In a blog post published on April 6, 2021, Google announced support for Rust within the Android Open Source Project as an alternative to C/C++.

On November 22, 2021, the Moderation Team, which was responsible for enforcing community standards and the Code of Conduct, announced their resignation "in protest of the Core Team placing themselves unaccountable to anyone but themselves". In May 2022, the Rust Core Team, other lead programmers, and certain members of the Rust Foundation board implemented governance reforms in response to the incident.

The Rust Foundation posted a draft for a new trademark policy on April 6, 2023, revising its rules on how the Rust logo and name can be used, which resulted in negative reactions from Rust users and contributors.

Syntax and features
Rust's syntax is similar to that of C and C++, although many of its features were influenced by functional programming languages such as OCaml. Hoare described Rust as targeted at "frustrated C++ developers" and emphasized features such as safety, control of memory layout, and concurrency. Safety in Rust includes the guarantees of memory safety, type safety, and lack of data races.

Hello World program
Below is a "Hello, World!" program in Rust. The keyword denotes a function, and the   macro prints the message to standard output. Statements in Rust are separated by semicolons.

Variables
Variables in Rust are defined through the keyword. The example below assigns a value to the variable with name foo.

Variables are immutable by default, and adding the keyword allows the variable to be mutated. The following example uses //, which denotes the start of a comment.

Multiple let expressions can define multiple variables with the same name, known as variable shadowing. Variable shadowing allows transforming variables without having to name the variables differently. The example below declares a new variable with the same name that is double the original value:

Variable shadowing is also possible for values of different types, going from a string to its length:

Keywords and control flow
In Rust, blocks of code are delimited by curly brackets.

An conditional expression executes code based on whether the given value is true. can be used for when the value evaluates to false, and can be used for combining multiple expressions.

The keyword allows repeating a portion of code until a  occurs. may optionally exit the loop with a value. Labels denoted with can be used to break an outer loop when loops are nested.

can be used to repeat a block of code while a condition is met. is used for looping over elements of a collection.

for loops and Iterators
For loops in Rust work in a functional style as operations over an iterator type. For example, in the loop

is a value of type  which implements the   trait; the code applies the function   to each element returned by the iterator. Iterators can be combined with functions over iterators like,  , and. For example, the following adds up all numbers between 1 and 100 that are multiples of 3:

Expressions
Rust is expression-oriented, with nearly every part of a function body being an expression, including control-flow operators. The ordinary  expression is used instead of C's ternary conditional. With returns being implicit, a function does not need to end with a  expression; if the semicolon is omitted, the value of the last expression in the function is used as 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:

Pattern matching
The and  expressions can be used for pattern matching. For example, can be used to double an optional integer value if present, and return zero otherwise:

Equivalently, this can be written with and :

Types
Rust is strongly typed and statically typed. The types of all variables must be known at compilation time; assigning a value of a particular type to a differently typed variable causes a compilation error. Type inference is used to determine the type of variables if unspecified.

The default integer type is, and the default floating point type is. If the type of a literal number is not explicitly provided, either it is inferred from the context or the default type is used.

User-defined types
User-defined types are created with the  or   keywords. The  keyword is used to denote a record type that groups multiple related values. s can take on different variants at runtime, with its capabilities similar to algebraic data types found in functional programming languages. Both structs and enums can contain fields with different types. Alternative names for the same type can be defined with the  keyword.

The  keyword can define methods for a user-defined type. Data and functions are defined separately. Implementations fulfill a role similar to that of classes within other languages.

Standard library
values are handled using syntactic sugar, such as the  construction, to access the inner value (in this case, a string):

Pointers
Rust does not use null pointers to indicate a lack of data, as doing so can lead to null dereferencing. Accordingly, the basic  and   references are guaranteed to not be null. Rust instead uses  for this purpose:   indicates that a value is present, and   is analogous to the null pointer. implements a "null pointer optimization", avoiding any spatial overhead for types that cannot have a null value (references or the  types, for example).

Unlike references, the raw pointer types  and   may be null; however, it is impossible to dereference them unless the code is explicitly declared unsafe through the use of an   block. Unlike dereferencing, the creation of raw pointers is allowed inside of safe Rust code.

Ownership and lifetimes
Rust's ownership system consists of rules that ensure memory safety without using a garbage collector. At compile time, each value must be attached to a variable called the owner of that value, and every value must have exactly one owner. Values are moved between different owners through assignment or passing a value as a function parameter. Values can also be borrowed, meaning they are temporarily passed to a different function before being returned to the owner. With these rules, Rust can prevent the creation and use of dangling pointers:

Because of these ownership rules, Rust types are known as linear or affine types, meaning each value can be used exactly once. This enforces a form of software fault isolation as the owner of a value is solely responsible for its correctness and deallocation.

When a value goes out of scope, it is dropped by running its destructor. The destructor may be programmatically defined through implementing the Drop trait. This helps manage resources such as file handles, network sockets, and locks, since when objects are dropped, the resources associated with them are closed or released automatically.

Lifetimes are usually an implicit part of all reference types in Rust. Each lifetime encompasses a set of locations in the code for which a variable is valid. For example, a reference to a local variable has a lifetime corresponding to the block it is defined in:

The borrow checker in the Rust compiler uses lifetimes to ensure that the values a reference points to remain valid. In the example above, storing a reference to variable x to r is valid, as variable x has a longer lifetime ('a) than variable r ('b). However, when x has a shorter lifetime, the borrow checker would reject the program:

Since the lifetime of the referenced variable ('b) is shorter than the lifetime of the variable holding the reference ('a), the borrow checker errors, preventing x from being used from outside its scope.

Lifetime parameters make the lifetimes of references explicit, for example, by specifying the source of an output:

With information regarding how lifetimes of the output value of the function related to its inputs, the compiler is able to prevent memory safety issues such as dangling pointers.

When user-defined types hold references to data, they also need to use lifetime parameters. The example below parses some configuration options from a string and creates a struct containing the options. The function  also showcases lifetime elision, which reduces the need for explicitly defining lifetime parameters.



Generics
Rust's more advanced features include the use of generic functions. A generic function is given generic parameters, which allow the same function to be applied to different variable types. This capability reduces duplicate code and is known as parametric polymorphism.

The following program calculates the sum of two things, for which addition is implemented using a generic function:

At compile time, polymorphic functions like  are instantiated with the specific types the code requires; in this case, sum of integers and sum of floats.

Generics can be used in functions to allow implementing a behavior for different types without repeating the same code. Generic functions can be written in relation to other generics, without knowing the actual type.

Traits
Rust's type system supports a mechanism called traits, inspired by type classes in the Haskell language, to define shared behavior between different types. For example, the  trait can be implemented for floats and integers, which can be added; and the   or   traits can be implemented for any type that can be converted to a string. Traits can be used to provide a set of common behavior for different types without knowing the actual type. This facility is known as ad hoc polymorphism.

Generic functions can constrain the generic type to implement a particular trait or traits; for example, an  function might require the type to implement. This means that a generic function can be type-checked as soon as it is defined. The implementation of 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. Type erasure is also available via the keyword  (short for dynamic). Because monomorphization duplicates the code for each type used, it can result in more optimized code for specific-use cases, but compile time and size of the output binary are also increased.

In addition to defining methods for a user-defined type, the  keyword can be used to implement a trait for a type. Traits can provide additional derived methods when implemented. For example, the trait  requires that the   method be defined for the type. Once the  method is defined, the trait can provide common functional helper methods over the iterator, such as   or.

Trait objects
Rust traits are implemented using static dispatch, meaning that the type of all values is known at compile time; however, Rust also uses a feature known as trait objects to accomplish dynamic dispatch (also known as duck typing). Dynamically dispatched trait objects are declared using the syntax  where   is a trait. Trait objects are dynamically sized, therefore they must be put behind a pointer, such as. The following example creates a list of objects where each object can be printed out using the  trait:

If an element in the list does not implement the  trait, it will cause a compile-time error.

Memory safety
Rust is designed to be memory safe. It does not permit null pointers, dangling pointers, or data races. Data values can be initialized only through a fixed set of forms, all of which require their inputs to be already initialized.

Unsafe code can subvert some of these restrictions, using the  keyword. Unsafe code may also be used for low-level functionality, such as volatile memory access, architecture-specific intrinsics, type punning, and inline assembly.

Memory management
Rust does not use garbage collection. Memory and other resources are instead managed through the "resource acquisition is initialization" convention, with optional reference counting. Rust provides deterministic management of resources, with very low overhead. Values are allocated on the stack by default, and all dynamic allocations must be explicit.

The built-in reference types using the  symbol do not involve run-time reference counting. The safety and validity of the underlying pointers is verified at compile time, preventing dangling pointers and other forms of undefined behavior. Rust's type system separates shared, immutable references of the form  from unique, mutable references of the form. A mutable reference can be coerced to an immutable reference, but not vice versa.

Macros
It is possible to extend the Rust language using macros.

Declarative macros
A declarative macro (also called a "macro by example") is a macro that uses pattern matching to determine its expansion.

Procedural macros
Procedural macros are Rust functions that run and modify the compiler's input token stream, before any other components are compiled. They are generally more flexible than declarative macros, but are more difficult to maintain due to their complexity.

Procedural macros come in three flavors:
 * Function-like macros
 * Derive macros
 * Attribute macros

The  macro is an example of a function-like macro. The macro provides 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  library for Rust bindings to R.

The following code shows the use of the,  , and  -derived procedural macros to implement JSON reading and writing, as well as the ability to format a structure for debugging.

Interface with C and C++
Rust has a foreign function interface (FFI) that can be used both to call code written in languages such as C from Rust and to call Rust code from those languages. , an external library called CXX exists for calling to or from C++. Rust and C differ in how they lay out structs in memory, so Rust structs may be given a  attribute, forcing the same layout as the equivalent C struct.

Ecosystem


The Rust ecosystem includes its compiler, its standard library, and additional components for software development. Component installation is typically managed by rustup, a Rust toolchain installer developed by the Rust project.

Compiler
The Rust compiler is named, and translates Rust code into a low level language called LLVM intermediate representation (LLVM IR). LLVM is then invoked as a subcomponent to translate IR code into machine code. A linker is then used to combine multiple crates together as a single executable or binary file.

Other than LLVM, the compiler also supports using alternative backends such as GCC and Cranelift for code generation. The intention of those alternative backends is to increase platform coverage of Rust or to improve compilation times.

Standard library
The Rust standard library defines and implements many widely used custom data types, including core data structures such as Vec, Option, and HashMap, as well as smart pointer types. Rust also provides a way to exclude most of the standard library using the attribute ; this enables applications, such as embedded devices, which want to remove dependency code or provide their own core data structures. Internally, the standard library is divided into three parts, core, alloc, and std, where std and alloc are excluded by.



Cargo
Cargo is Rust's build system and package manager. It downloads, compiles, distributes, and uploads packages—called crates—that are maintained in an official registry. It also acts as a front-end for Clippy and other Rust components.

By default, Cargo sources its dependencies from the user-contributed registry crates.io, but Git repositories and crates in the local filesystem, and other external sources can also be specified as dependencies.

Rustfmt
Rustfmt is a code formatter for Rust. It formats whitespace and indentation to produce code in accordance with a common style, unless otherwise specified. It can be invoked as a standalone program, or from a Rust project through Cargo.



Clippy
Clippy is Rust's built-in linting tool to improve the correctness, performance, and readability of Rust code. , it has more than 700 rules.

Versioning system
Following Rust 1.0, new features are developed in nightly versions which are released daily. During each six-week release cycle, changes to nightly versions are released to beta, while changes from the previous beta version are released to a new stable version.

Every two or three years, a new "edition" is produced. Editions are released to allow making limited breaking changes, such as promoting await to a keyword to support async/await features. Crates targeting different editions can interoperate with each other, so a crate can upgrade to a new edition even if its callers or its dependencies still target older editions. Migration to a new edition can be assisted with automated tooling.

IDE support
rust-analyzer is a collection of utilities that provides Integrated development environments (IDEs) and text editors with information about a Rust project through the Language Server Protocol. This enables features including autocompletion, and the display of compilation errors while editing.

Performance
In general, Rust's memory safety guarantees do not impose a runtime overhead. A notable exception is array indexing which is checked at runtime, though this often does not impact performance. Since it does not perform garbage collection, Rust is often faster than other memory-safe languages.

Rust provides two "modes": safe and unsafe. Safe mode is the "normal" one, in which most Rust is written. In unsafe mode, the developer is responsible for the code's memory safety, which is used by developers for cases where the compiler is too restrictive.

Many of Rust's features are so-called zero-cost abstractions, meaning they are optimized away at compile time and incur no runtime penalty. The ownership and borrowing system permits zero-copy implementations for some performance-sensitive tasks, such as parsing. Static dispatch is used by default to eliminate method calls, with the exception of methods called on dynamic trait objects. The compiler also uses inline expansion to eliminate function calls and statically-dispatched method invocations.

Since Rust utilizes LLVM, any performance improvements in LLVM also carry over to Rust. Unlike C and C++, Rust allows for reordering struct and enum elements to reduce the sizes of structures in memory, for better memory alignment, and to improve cache access efficiency.

Adoption
Rust has been used in software across different domains. Rust was initially funded by Mozilla as part of developing Servo, an experimental parallel browser engine, in collaboration with Samsung. Components from the Servo engine were later incorporated in the Gecko browser engine underlying Firefox. In January 2023, Google (Alphabet) announced support for using third party Rust libraries in Chromium.

Rust is used in several backend software projects of large web services. OpenDNS, a DNS resolution service owned by Cisco, uses Rust internally. Amazon Web Services uses Rust in "performance-sensitive components" of its several services. In 2019, AWS has open-sourced Firecracker, a virtualization solution primarily written in Rust. Microsoft Azure IoT Edge, a platform used to run Azure services on IoT devices, has components implemented in Rust. Microsoft also uses Rust to run containerized modules with WebAssembly and Kubernetes. Cloudflare, a company providing content delivery network services, used Rust to build a new web proxy named Pingora for increased performance and efficiency.

In operating systems, Rust support has been added to Linux and Android. Microsoft is rewriting parts of Windows in Rust. The r9 project aims to re-implement Plan 9 from Bell Labs in Rust. Rust has been used in the development of new operating systems such as Redox, a "Unix-like" operating system and microkernel, Theseus, an experimental operating system with modular state management, and most of Fuchsia. Rust is also used for command-line tools and operating system components, including stratisd, a file system manager and COSMIC, a desktop environment by System76.



In web development, the npm package manager started using Rust in production in 2019. Deno, a secure runtime for JavaScript and TypeScript, is built on top of V8 using Rust and Tokio. Other notable adoptions in this space include Ruffle, an open-source SWF emulator, and Polkadot, an open source blockchain and cryptocurrency platform.

Discord, an instant messaging software company, has rewritten parts of its system in Rust for increased performance in 2020. In the same year, Dropbox announced that its file synchronization had been rewritten in Rust. Facebook (Meta) has also used Rust to redesign its system that manages source code for internal projects.

In the 2023 Stack Overflow Developer Survey, 13% of respondents had recently done extensive development in Rust. The survey also named Rust the "most loved programming language" every year from 2016 to 2023 (inclusive), based on the number of developers interested in continuing to work in the same language. In 2023, Rust was the 6th "most wanted technology", with 31% of developers not currently working in Rust expressing an interest in doing so.

In academic research
Rust has been studied in academic research, both for properties of the language itself as well as the utility the language provides for writing software used for research. Its features around safety and performance have been examined.

In a journal article published to Proceedings of the International Astronomical Union, astrophysicists Blanco-Cuaresma and Bolmont re-implemented programs responsible for simulating multi-planet systems in Rust, and found it to be a competitive programming language for its "speed and accuracy". Likewise, an article published on Nature shared several stories of bioinformaticians using Rust for its performance and safety. However, both articles have cited Rust's unique concepts, including its ownership system, being difficult to learn as one of the main drawbacks to adopting Rust.

Community


Rust has been noted as having an inclusive community, and particularly welcomed people from the queer community, partly due to its code of conduct which outlined a set of expectations for Rust community members to follow. One MIT Technology Review article described the Rust community as "unusually friendly" to newcomers.

Rust Foundation
The Rust Foundation is a non-profit membership organization incorporated in United States, with the primary purposes of backing the technical project as a legal entity and helping to manage the trademark and infrastructure assets.

It was established on February 8, 2021, with five founding corporate members (Amazon Web Services, Huawei, Google, Microsoft, and Mozilla). The foundation's board is chaired by Shane Miller. Starting in late 2021, its Executive Director and CEO is Rebecca Rumbul. Prior to this, Ashley Williams was interim executive director.

Governance teams
The Rust project is composed of teams that are responsible for different subareas of the development. The compiler team develops, manages, and optimizes compiler internals; and the language team designs new language features and helps implement them. The Rust project website lists 6 top-level teams. Representatives among teams form the Leadership council, which oversees the Rust project as a whole.