The official Rust logo | |
Paradigms | Multi-paradigm: concurrent, functional, generic, imperative, structured |
---|---|
Designed by | Graydon Hoare |
First appeared | July 7, 2010 |
Stable release | 1.66.1[1] ![]() |
Typing discipline | Affine, inferred, nominal, static, strong |
Implementation language | Rust |
Platform | Cross-platform[note 1] |
OS | Cross-platform[note 2] |
License | MIT and Apache 2.0 (dual-licensed)[note 3] |
Filename extensions | .rs , .rlib |
Website | www |
Influenced by | |
Influenced | |
Idris,[6] Spark,[7] Swift,[8] Project Verona,[9] Zig[10] |
Rust is a multi-paradigm, high-level, general-purpose programming language. Rust emphasizes performance, type safety, and concurrency. Rust enforces memory safety—that is, that all references point to valid memory—without requiring the use of a garbage collector or reference counting present in other memory-safe languages. To simultaneously enforce memory safety and prevent concurrent data races, Rust's "borrow checker" tracks the object lifetime of all references in a program during compilation. Rust is popular for systems programming but also offers high-level features including some functional programming constructs.[11]
Software developer Graydon Hoare created Rust as a personal project while working at Mozilla Research in 2006. Mozilla officially sponsored the project in 2009. Since the first stable release in May 2015, Rust has been adopted by companies including Amazon, Discord, Dropbox, Facebook (Meta), Google (Alphabet), and Microsoft.
Rust has been noted for its growth as a newer language[12][13] and has been the subject of academic programming languages research.[14][15][16]
Rust grew out of a personal project begun in 2006 by Mozilla Research employee Graydon Hoare. Mozilla began sponsoring the project in 2009 as a part of the ongoing development of an experimental browser engine called Servo.[17] The project was officially announced by Mozilla in 2010.[18][19] During the same year, work had 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.[17]
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.[20] Four months later, version 0.3 added destructors and polymorphism through the use of interfaces.[21] In October 2012, version 0.4 was released and added traits as a means for inheritance. Interfaces were unified with traits and removed as a separate feature, and classes were replaced by a combination of implementations and structured types.[22] Prior to version 0.4, Rust also supported typestate analysis through contracts. It was removed in release 0.4, though the same functionality can be achieved by leveraging Rust's type system.[23]
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++ in addition to the 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.[24] The first stable release, Rust 1.0, was announced on May 15, 2015.[25][26]
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.[27][28] The team behind Servo, a browser engine written in Rust, was completely disbanded. The event raised concerns about the future of Rust, as some members of the team were active contributors to Rust.[29] 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.[30]
On February 8, 2021, the formation of the Rust Foundation was announced by its five founding companies (AWS, Huawei, Google, Microsoft, and Mozilla).[31][32] In a blog post published on April 6, 2021, Google announced support for Rust within Android Open Source Project as an alternative to C/C++.[33][34]
On November 22, 2021, the Moderation team, 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."[35] In May 2022, the Rust core team, other leads, and certain members of the Rust Foundation board sent out a statement with governance reforms in response to the incident.[36]
Below is a "Hello, World!" program in Rust. The fn
keyword is used to denote a function, and the println!
macro prints the message to standard output.[37] Statements in Rust are separated by semicolons.
fn main() {
println!("Hello, World!");
}
In Rust, blocks of code are delimited by curly brackets, and control flow is annotated with keywords such as if
, else
, while
, and for
.[38] Pattern matching can be done using the match
keyword.[39] In the examples below, explanations are given in comments, which start with //
.[40]
fn main() {
let mut values = vec![1, 2, 3, 4];
for value in &values {
println!("value = {}", value);
}
if values.len() > 5 {
println!("List is longer than five items");
}
// Pattern matching
match values.len() {
0 => println!("Empty"),
1 => println!("One value"),
2..=10 => println!("Between two and ten values"),
11 => println!("Eleven values"),
_ => println!("Many values"),
};
// while loop with predicate and pattern matching using let
while let Some(value) = values.pop() {
println!("value = {value}"); // using curly braces to format a local variable
}
}
Despite its syntactic resemblance to C and C++,[41][42] Rust is more significantly influenced by functional programming languages,[43] including Standard ML, OCaml, Haskell, and Erlang.[5] For example, nearly every part of a function body is an expression, even control flow operators.[44] The ordinary if
expression also takes the place of C's ternary conditional. A function does not need to end with a return
expression: if the semicolon is omitted, the value of the last expression in the function will be used as the return value,[45] as seen in the following recursive implementation of the factorial function:
fn factorial(i: u64) -> u64 {
if i == 0 {
1
} else {
i * factorial(i - 1)
}
}
The following iterative implementation uses the ..=
operator to create an inclusive range:
fn factorial(i: u64) -> u64 {
(2..=i).product()
}
Rust is strongly typed and statically typed: all types of variables must be known during compilation, and assigning a value of a different type to a variable will result in a compilation error. The default integer type is i32
, and the default floating point type is f64
. If the type of a literal number is not explicitly provided, either it is inferred from the context or the default type is used.[46]
Type | Description | Examples |
---|---|---|
bool
|
Boolean value |
|
u8
|
Unsigned 8-bit integer (a byte) | |
|
Signed integers, up to 128 bits | |
|
Unsigned integers, up to 128 bits | |
|
Pointer-sized integers (size depends on platform) | |
|
Floating-point numbers |
|
char
|
|
|
&str
|
Strings (static or immutable) |
|
[T; N]
|
Static arrays (size known at compile-time) |
|
[T]
|
Static arrays (size not known at compile-time) |
|
|
Tuples |
|
!
|
Never type (unreachable value) | let x = { return 123 };
|
Type | Description | Examples |
---|---|---|
Box<T>
|
Values in the heap | Box::new(5)
|
String
|
Strings (dynamic) |
|
Vec<T>
|
Dynamic arrays |
|
Option<T>
|
Option type |
|
Result<T, E>
|
Error handling |
|
Option
values are handled using syntactic sugar, such as the if let
construction, in order to access the inner value (in this case, a string):[48]
fn main() {
let name: Option<String> = None;
// If name was not None, it would print here.
if let Some(name) = name {
println!("{}", name);
}
}
Type | Description | Examples |
---|---|---|
|
References (immutable and mutable) |
|
|
|
|
|
|
|
Unlike other languages, Rust does not use null pointers to indicate a lack of data, as doing so can lead to null dereferencing. Accordingly, the basic &
and &mut
references are guaranteed to not be null. Rust instead uses Option
for this purpose: Some(T)
indicates that a value is present and None
is analogous to the null pointer.[49] Option
implements a "null pointer optimization" avoiding any overhead for types which cannot have a null value (references or the NonZero
types, for example).
Unlike references, the raw pointer types *const
and *mut
may be null; however, it is impossible to dereference them unless the code is explicitly declared unsafe through the use of an unsafe
block.[50]
More advanced features in Rust include the use of generic functions to reduce duplicate code.[51] This capability is called parametric polymorphism. The following is a Rust program to calculate the sum of two things, for which addition is implemented using a generic function:
use std::ops::Add;
// sum is a generic function with one type parameter, T
fn sum<T>(num1: T, num2: T) -> T
where
T: Add<Output = T>, // T must implement the Add trait where addition returns another T
{
num1 + num2 // num1 + num2 is syntactic sugar for num1.add(num2) provided by the Add trait
}
fn main() {
let result1 = sum(10, 20);
println!("Sum is: {}", result1); // Sum is: 30
let result2 = sum(10.23, 20.45);
println!("Sum is: {}", result2); // Sum is: 30.68
}
At compile-time, polymorphic functions like sum
are instantiated with the specific types that are needed by the code (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.[52]
Rust's ownership system consists of rules that ensure memory safety without using a garbage collector. In the system, each value in Rust must be attached to a variable called the owner of that value, and every value must have exactly one owner.[53] Values are moved between different owners through assignment or passing a value as a function parameter. Values can also be borrowed, meaning that they are temporarily passed to a different function before being returned to the owner.[54] With these rules, Rust can prevent the creation and use of dangling pointers:[54][55]
fn print_string(s: String) {
println!("{}", s);
}
fn main() {
let s = String::from("Hello, World");
print_string(s); // s consumed by print_string
// s has been moved, so cannot be used any more
// another print_string(s); would result in a compile error
}
Lifetimes are a usually implicit part of all reference types in Rust. Each particular lifetime encompasses a set of locations in the code for which a variable is valid. The borrow checker in the Rust compiler uses lifetimes to ensure that the values pointed to by a reference remain valid. It also ensures that a mutable reference only exists if no immutable references exist at the same time.[56][57] Rust's memory and ownership system was influenced by region-based memory management in languages such as Cyclone and ML Kit.[5]
Rust defines the relationship between the lifetimes of the objects used and created by functions as part of their signature using lifetime parameters.[58]
When a stack variable or temporary goes out of scope, it is dropped by running its destructor. The destructor may be programmatically defined through the drop
function. This structure enforces the so-called resource acquisition is initialization (RAII) design pattern, in which resources, like file descriptors or network sockets, are tied to the lifetime of an object: when the object is dropped, the resource is closed.[59][60]
The example below parses some configuration options from a string and creates a struct containing the options. The struct only contains references to the data, so for the struct to remain valid, the data referred to by the struct needs to be valid as well. The function signature for parse_config
specifies this relationship explicitly. In this example, the explicit lifetimes are unnecessary in newer Rust versions due to lifetime elision, which is an algorithm that automatically assigns lifetimes to functions if they are trivial.[61]
use std::collections::HashMap;
// This struct has one lifetime parameter, 'src. The name is only used within the struct's definition.
#[derive(Debug)]
struct Config<'src> {
hostname: &'src str,
username: &'src str,
}
// This function also has a lifetime parameter, 'cfg. 'cfg is attached to the "config" parameter, which
// establishes that the data in "config" lives at least as long as the 'cfg lifetime.
// The returned struct also uses 'cfg for its lifetime, so it can live at most as long as 'cfg.
fn parse_config<'cfg>(config: &'cfg str) -> Config<'cfg> {
let key_values: HashMap<_, _> = config
.lines()
.filter(|line| !line.starts_with('#'))
.filter_map(|line| line.split_once('='))
.map(|(key, value)| (key.trim(), value.trim()))
.collect();
Config {
hostname: key_values["hostname"],
username: key_values["username"],
}
}
fn main() {
let config = parse_config(
r#"hostname = foobar
username=barfoo"#,
);
println!("Parsed config: {:#?}", config);
}
Rust aims to support concurrent systems programming, which has inspired a feature set with an emphasis on safety, control of memory layout, and concurrency.[62] Safety in Rust includes the guarantees of memory safety, type safety, and lack of data races.
Rust is designed to be memory safe. It does not permit null pointers, dangling pointers, or data races.[63][64][65] Data values can be initialized only through a fixed set of forms, all of which require their inputs to be already initialized.[66] To replicate pointers being either valid or NULL
, such as in linked list or binary tree data structures, the Rust core library provides an option type, which can be used to test whether a pointer has Some
value or None
.[64] 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.[50] Unsafe code may also be used for low-level functionality like volatile memory access, architecture-specific intrinsics, type punning, and inline assembly.[48]: 139, 376–379, 395
Rust does not use automated garbage collection. Memory and other resources are managed through the "resource acquisition is initialization" convention,[67] with optional reference counting. Rust provides deterministic management of resources, with very low overhead.[68] Values are allocated on the stack by default and all dynamic allocations must be explicit.[69]
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.[70] Rust's type system separates shared, immutable references of the form &T
from unique, mutable references of the form &mut T
. A mutable reference can be coerced to an immutable reference, but not vice versa.[71]
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, floats and integers both implement the Add
trait because they can both be added; and any type that can be converted to a string implements the Display
or Debug
traits. This facility is known as ad hoc polymorphism.
Rust uses type inference for variables declared with the keyword let
. 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.[72] Variables assigned multiple times must be marked with the keyword mut
(short for mutable).[73]
A function can be given generic parameters, which allows the same function to be applied to different types. Generic functions can constrain the generic type to implement a particular trait or traits; for example, an add_one
function might require the type to implement Add
. 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. Type erasure is also available in Rust via the keyword dyn
(short for dynamic).[74] 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.[75]
In Rust, user-defined types are created with the struct
or enum
keywords. The struct
keyword is used to denote a record type that groups multiple related values.[76] enum
s can take on different variants in runtime, with its capabilities similiar to algebraic data types found in functional programming languages.[77] Both structs and enums can contain fields with different types.[78] The impl
keyword can define methods for the types (data and functions are defined separately) or implement a trait for the types.[79] Traits can be used to provide a set of common behavior for different types without knowing the actual type, and can provide additional methods when implemented.[80] For example, the trait Iterator
requires that the next
method be defined for the type. Once the next
method is defined, the trait can provide common functional helper methods over the iterator like map
or filter
.[81]
Type aliases, including generic arguments, can also be defined with the type
keyword.[82]
The type 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. Rust supports interface inheritance but replaces implementation inheritance with composition.[83]
Rust uses linear types, where each value is used exactly once, to enforce type safety. This enables software fault isolation with a low overhead.[84]
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).[85] Dynamically dispatched trait objects are declared using the syntax Box<dyn Tr>
where Tr
is a trait. For example, it is possible to create a list of objects which each can be printed out as follows: let v: Vec<Box<dyn Display>> = vec![Box::new(3), Box::new(5.0), Box::new("hi")]
.[85] Trait objects are dynamically sized; however, prior to the 2018 edition, the dyn
keyword was optional.[86] A trait object is essentially a fat pointer that include a pointer as well as additional information about what type the pointer is.[87]
It is possible to extend the Rust language using macros.
A declarative macro (also called a "macro by example") is a macro that uses pattern matching to determine its expansion.[88]
Procedural macros use Rust functions that are compiled before other components to run and modify the compiler's input token stream. They are generally more flexible than declarative macros, but are more difficult to maintain due to their complexity.[89][90]
Procedural macros come in three flavors:
custom!(...)
#[derive(CustomDerive)]
#[custom_attribute]
The println!
macro is an example of a function-like macro and serde_derive
[91] 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.[92]
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.
use serde_json::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
println!("serialized = {}", serialized);
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
println!("deserialized = {:?}", deserialized);
}
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. Rust also has a library, CXX, for calling to or from C++.[93] Rust and C differ in how they lay out structs in memory, so Rust structs may be given a #[repr(C)]
attribute, forcing the same layout as the equivalent C struct.[94]
Besides the compiler and standard library, the Rust ecosystem includes additional components for software development. Component installation is typically managed by rustup
, a Rust toolchain installer developed by the Rust project.[95]
The Rust standard library is split into three crates: core
, alloc
, and std
. When a project is annotated with the crate-level attribute #![no_std]
, the std
crate is excluded.[96]
Cargo is Rust's build system and package manager. Cargo downloads, compiles, distributes, and uploads packages, called crates, maintained in the official registry. Cargo also acts as a front-end for Clippy and other Rust components.[12]
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 be specified as dependencies, too.[97]
Rustfmt is a code formatter for Rust. It takes Rust source code as input and changes the whitespace and indentation to produce code formatted in accordance to a common style unless specified otherwise. Rustfmt can be invoked as a standalone program or on a Rust project through Cargo.[98]
Clippy is Rust's built-in linting tool to improve the correctness, performance, and readability of Rust code. It was created in 2014[99] and named after the eponymous Microsoft Office feature.[100] As of 2021, Clippy has more than 450 rules,[101] which can be browsed online and filtered by category.[102][103]
Following Rust 1.0, new features are developed in nightly versions which release on a daily basis. During each release cycle of six weeks, changes on nightly versions are released to beta, while changes from the previous beta version are released to a new stable version.[104]
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. Editions are largely compatible and migration to a new edition can be assisted with automated tooling.[105]
The most popular language server for Rust is rust-analyzer. The original language server, RLS was officially deprecated in favor of rust-analyzer in July 2022.[106] These projects provide IDEs and text editors with more information about a Rust project, with basic features including autocompletion, and display of compilation errors while editing.[citation needed]
Rust aims "to be as efficient and portable as idiomatic C++, without sacrificing safety".[107] Rust does not perform garbage collection, which allows it to be more efficient and performant than other memory-safe languages.[108][109][110]
Rust provides two "modes": safe and unsafe. The safe mode is the "normal" one, in which most Rust is written. In unsafe mode, the developer is responsible for the correctness of the code, making it possible to create applications which require low-level features.[111] It has been demonstrated empirically that unsafe Rust is not always more performant than safe Rust, and can even be slower in some cases.[112]
Many of Rust's features are so-called zero-cost abstractions, meaning they are optimized away at compile time and incur no runtime penalty.[48]: 19,27 The ownership and borrowing system permits zero-copy implementations for some performance-sensitive tasks, such as parsing.[113] Static dispatch is used by default to eliminate method calls, with the exception of methods called on dynamic trait objects.[48]: 20 The compiler also uses inline expansion to eliminate function calls and statically dispatched method invocations entirely.[114]
Since Rust utilizes LLVM, any performance improvements in LLVM also carry over to Rust.[115] Unlike C and C++, Rust allows re-organizing struct and enum element ordering.[116] This can be done to reduce the size of structures in memory, for better memory alignment, and to improve cache access efficiency.[117]
According to the Stack Overflow Developer Survey in 2022, 9% of respondents have recently done extensive development in Rust.[118] The survey has additionally named Rust the "most loved programming language" every year from 2016 to 2022 (inclusive), a ranking based on the number of current developers who express an interest in continuing to work in the same language.[119][note 7] In 2022, Rust tied with Python for "most wanted technology" with 18% of developers not currently working in Rust expressing an interest in doing so.[118][120]
Rust has been adopted for components at a number of major software companies, including Amazon,[121][122] Discord,[123] Dropbox,[124] Facebook (Meta),[125] Google (Alphabet),[126][33] and Microsoft.[127][128]
Rust's official website lists online forums, messaging platforms, and in-person meetups for the Rust community.[155] Conferences dedicated to Rust development include:
Formation | February 8, 2021 |
---|---|
Founders | |
Type | Nonprofit organization |
Location | |
Shane Miller | |
Rebecca Rumbul | |
Website | 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.[162][42]
It was established on February 8, 2021, with five founding corporate members (Amazon Web Services, Huawei, Google, Microsoft, and Mozilla).[163] The foundation's board is chaired by Shane Miller.[164] Starting in late 2021, its Executive Director and CEO is Rebecca Rumbul.[165] Prior to this, Ashley Williams was interim executive director.[166]
The Rust project is composed of teams that are responsible for different subareas of the development. For example, the Core team is responsible for "managing the overall direction of Rust, subteam leadership, and any cross-cutting issues," the Compiler team is responsible for "developing and managing compiler internals and optimizations," and the Language team is responsible for "designing and helping to implement new language features," according to the official website.[167][168]