Paradigms | |
---|---|
Designed by | Graydon Hoare |
Developer | The Rust Team |
First appeared | May 15, 2015 |
Stable release | |
Typing discipline | |
Implementation language | OCaml (2006–2011) Rust (2012–present) |
Platform | Cross-platform [note 1] |
OS | Cross-platform [note 2] |
License | MIT, Apache 2.0 [note 3] |
Filename extensions | .rs , .rlib |
Website | www |
Influenced by | |
Influenced | |
Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory. It does so without a traditional garbage collector; instead, memory safety errors and data races are prevented by the "borrow checker", which tracks the object lifetime of references at compile time.
Rust does not enforce a programming paradigm, but was influenced by ideas from functional programming, including immutability, higher-order functions, algebraic data types, and pattern matching. It also supports object-oriented programming via structs, enums, traits, and methods. It is popular for systems programming. [13] [14] [15]
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. [16]
Rust has been noted for its rapid adoption, and has been studied in programming language theory research.
Rust began as a personal project in 2006 by Mozilla employee Graydon Hoare. [17] Hoare has stated that Rust was named for the group of fungi that are "over-engineered for survival". [17] During the time period between 2006 and 2009, Rust was not publicized to others at Mozilla and was written in Hoare's free time; [18] Hoare began speaking about the language around 2009 after a small group at Mozilla became interested in the project. [19] Hoare emphasized prioritizing good ideas from old languages over new development, citing languages including CLU (1974), BETA (1975), Mesa (1977), NIL (1981), Erlang (1987), Newsqueak (1988), Napier (1988), Hermes (1990), Sather (1990), Alef (1992), and Limbo (1996) as influences, stating "many older languages [are] better than new ones", and describing the language as "technology from the past come to save the future from itself." [18] [19] Early Rust developer Manish Goregaokar similarly described Rust as being based on "mostly decades-old research." [17]
During the early years, the Rust compiler was written in about 38,000 lines of OCaml. [18] [20] Early Rust contained features such as explicit object-oriented programming via an obj
keyword (later removed), and a system for something called typestates that would allow variables of a type to be tracked along with state changes (such as going from uninitialized to initialized). Functions were pure by default, meaning that side effects (such as reading or writing to a file) were not allowed without an explicit annotation. [18]
Mozilla officially sponsored the Rust project in 2009. [17] Brendan Eich and other executives, intrigued by the possibility of using Rust for a safe web browser engine, placed engineers on the project including Patrick Walton, Niko Matsakis, Felix Klock, and Manish Goregaokar. A conference room taken by the project developers was dubbed "the nerd cave," with a sign placed outside the door. [17]
During this time period, work had shifted from the initial OCaml compiler to a self-hosting compiler, i.e., written in Rust, based on LLVM. [21] [note 4] The Rust ownership system was also in place by 2010. [17] The Rust logo was developed in 2011 based on a bicycle chainring. [23]
The first public release, Rust 0.1 was released in January 2012. [21] [ better source needed ] The early 2010s saw increasing involvement from open source volunteers outside of Mozilla and outside of the United States. At Mozilla, executives would eventually employ over a dozen engineers to work on Rust full time over the next decade. [17]
The years from 2012 to 2015 were marked by substantial changes to the Rust type system, especially, removal of the typestate system, consolidation of other language features, and the removal of the garbage collector. [18] [17] Memory management through the ownership system was gradually consolidated and expanded to prevent memory-related bugs. By 2013, the garbage collector feature was rarely used, and was removed by the team in favor of the ownership system. [17] Other changes during this time included the removal of pure functions, which were declared by an explicit pure
annotation, in March 2013. [24] Specialized syntax support for channels and various pointer types were removed to simplify the language. [18]
Rust's expansion and consolidation was influenced by developers coming from C++ (e.g., low-level performance of features), scripting languages (e.g., Cargo and package management), and functional programming (e.g., type systems development). [18]
Graydon Hoare stepped down from Rust in 2013. [17] This allowed it to evolve organically under a more federated governance structure, with a "core team" of initially 6 people, 30-40 developers total across various other teams, and a Request for Comments (RFC) process for new language features added in March 2014. [18] The core team would grow to 9 people by 2016 with over 1600 proposed RFCs. [18]
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. [25] Rust development at this time was focused on finalizing the language features and moving towards 1.0 so it could achieve backward compatibility and productize the language for potential industry adoption. [18]
Six years after Mozilla sponsored its development, the first stable release, Rust 1.0, was published on May 15, 2015. [17] A year after the release, the Rust compiler had accumulated over 1,400 contributors and there were over 5,000 third-party libraries published on the Rust package management website Crates.io. [18]
The development of the Servo browser engine continued in parallel with Rust, jointly funded by Mozilla and Samsung. [26] The teams behind the two projects worked in close collaboration; new features in Rust were tested out by the Servo team, and new features in Servo were used to give feedback back to the Rust team. [18] The first version of Servo was released in 2016. [17] The Firefox web browser shipped with Rust code as of 2016 (version 45), [18] [27] but components of Servo did not appear in Firefox until September 2017 (version 57) as part of the Gecko and Quantum projects. [28]
Improvements were made to the Rust toolchain ecosystem during the years following 1.0 including Rustfmt, integrated development environment integration, a regular compiler testing and release cycle, a community code of conduct, and community discussion organized through an IRC chat. [18]
The earliest adoption outside of Mozilla was by individual projects at Samsung, Facebook (now Meta Platforms), Dropbox, and others including Tilde, Inc. (the company behind ember.js). [18] [17] Amazon Web Services followed in 2020. [17] Engineers cited performance, lack of a garbage collector, safety, and pleasantness of working in the language as reasons for the adoption, while acknowledging that it was a risky bet as Rust was new technology. Amazon developers cited the fact that Rust uses half as much electricity as similar code written in Java, behind only C, [17] as found by a study at the University of Minho, NOVA University Lisbon, and the University of Coimbra. [29] [note 5]
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. [30] [31] The team behind Servo was disbanded. The event raised concerns about the future of Rust, due to the overlap between the two projects. [32] 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. [33]
On February 8, 2021, the formation of the Rust Foundation was announced by five founding companies: Amazon Web Services, Google, Huawei, Microsoft, and Mozilla. [34] [35] The foundation, led by Shane Miller for its first two years, offered $20,000 grants and other support for programmers working on major Rust features. [17] 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++. [36]
On November 22, 2021, the Moderation Team, which was responsible for enforcing the community code of conduct, announced their resignation "in protest of the Core Team placing themselves unaccountable to anyone but themselves". [37] 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. [38]
The Rust Foundation posted a draft for a new trademark policy on April 6, 2023, including rules for how the Rust logo and name can be used, which resulted in negative reactions from Rust users and contributors. [39]
Rust's syntax is similar to that of C and C++, [40] [41] although many of its features were influenced by functional programming languages such as OCaml. [42] Hoare has described Rust as targeted at frustrated C++ developers and emphasized features such as safety, control of memory layout, and concurrency. [19] Safety in Rust includes the guarantees of memory safety, type safety, and lack of data races.
Below is a "Hello, World!" program in Rust. The fn
keyword denotes a function, and the println!
macro (see § Macros) prints the message to standard output. [43] Statements in Rust are separated by semicolons.
fnmain(){println!("Hello, World!");}
Variables in Rust are defined through the let
keyword. [44] The example below assigns a value to the variable with name foo
.
fnmain(){letfoo=10;println!("The value of foo is {foo}");}
Variables are immutable by default, and adding the mut
keyword allows the variable to be mutated. [45] The following example uses //
, which denotes the start of a comment. [46]
fnmain(){letmutfoo=10;// This code would not compile without adding "mut".println!("The value of foo is {foo}");foo=20;println!("The value of foo is {foo}");}
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. [47] The example below declares a new variable with the same name that is double the original value:
fnmain(){letfoo=10;println!("The value of foo is {foo}");letfoo=foo*2;println!("The value of foo is {foo}");}
Variable shadowing is also possible for values of different types, going from a string to its length:
fnmain(){letspaces=" ";letspaces=spaces.len();}
In Rust, blocks of code are delimited by curly brackets. [48]
if
blocksAn if
conditional expression executes code based on whether the given value is true
. else
can be used for when the value evaluates to false
, and elseif
can be used for combining multiple expressions. [49]
fnmain(){letx=10;ifx>5{println!("value is greater than five");}ifx%7==0{println!("value is divisible by 7");}elseifx%5==0{println!("value is divisible by 5");}else{println!("value is not divisible by 7 or 5");}}
while
loops while
can be used to repeat a block of code while a condition is met. [50]
fnmain(){// Iterate over all integers from 4 to 10letmutvalue=4;whilevalue<=10{println!("value = {value}");value+=1}}
for
loops and iteratorsFor loops in Rust loop over elements of a collection. [51] "For" expressions work over any iterator type.
fnmain(){// Using `for` with range syntax for the same functionality as aboveforvaluein4..=10{println!("value = {value}");}}
In the above code, 4..=10
is a value of type Range
which implements the Iterator
trait. The code within the curly braces is applied to each element returned by the iterator.
Iterators can be combined with functions over iterators like map
, filter
, and sum
. For example, the following adds up all numbers between 1 and 100 that are multiples of 3:
(1..=100).filter(|&x|x%3==0).sum()
loop
and break
statementsMore generally, the loop
keyword allows repeating a portion of code until a break
occurs. break
may optionally exit the loop with a value. Labels denoted with 'label_name
can be used to break an outer loop when loops are nested. [52]
fnmain(){letvalue=456;letmutx=1;lety=loop{x*=10;ifx>value{breakx/10;}};println!("largest power of ten that is smaller than or equal to value: {y}");letmutup=1;'outer: loop{letmutdown=120;loop{ifup>100{break'outer;}ifdown<4{break;}down/=2;up+=1;println!("up: {up}, down: {down}");}up*=2;}}
Rust is expression-oriented, with nearly every part of a function body being an expression, including control-flow operators. [53] The if
expression is used to provide the ternary conditional operator. With returns being implicit, 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 is used as the return value, [54] as seen in the following recursive implementation of the factorial function:
fnfactorial(i: u64)-> u64{ifi==0{1}else{i*factorial(i-1)}}
The following iterative implementation uses the ..=
operator to create an inclusive range:
fnfactorial(i: u64)-> u64{(2..=i).product()}
The match
and iflet
expressions can be used for pattern matching. For example, match
can be used to double an optional integer value if present, and return zero otherwise: [55]
fndouble(x: Option<u64>)-> u64{matchx{Some(x)=>x*2,None=>0,}}
Equivalently, this can be written with iflet
and else
:
fndouble(x: Option<u64>)-> u64{ifletSome(x)=x{x*2}else{0}}
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. [56]
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. [57]
Integer types in Rust are named based on the signedness and the number of bits the type takes. For example, i32
is a signed integer that takes 32 bits of storage, whereas u8
is unsigned and only takes 8 bits of storage. isize
and usize
take storage depending on the architecture of the computer that runs the code, for example, on computers with 32-bit architectures, both types will take up 32 bits of space.
By default, integer literals are in base-10, but different radices are supported with prefixes, for example, 0b11
for binary numbers, 0o567
for octals, and 0xDB
for hexadecimals. By default, integer literals default to i32
as its type. Suffixes such as 4u32
can be used to explicitly set the type of a literal. [58] Byte literals such as b'X'
are available to represent the ASCII value (in u8
) of a specific character. [59]
The Boolean type is referred to as bool
which can take a value of either true
or false
. A char
takes up 32 bits of space and represents a Unicode scalar value: a Unicode codepoint that is not a surrogate. [60] IEEE 754 floating point numbers are supported with f32
for single precision floats and f64
for double precision floats. [61]
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. [62] enum
s can take on different variants at runtime, with its capabilities similar to algebraic data types found in functional programming languages. [63] Both structs and enums can contain fields with different types. [64] Alternative names for the same type can be defined with the type
keyword. [65]
The impl
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. [66]
Type | Description | Examples |
---|---|---|
String | UTF-8-encoded strings (dynamic) |
|
| Platform-native strings [note 6] (borrowed [67] and dynamic [68] ) |
|
| Paths (borrowed [69] and dynamic [70] ) |
|
| C-compatible, null-terminated strings (borrowed [71] and dynamic [71] ) |
|
Vec<T> | Dynamic arrays |
|
Option<T> | Option type |
|
Result<T,E> | Error handling using a result type |
|
Box<T> | A pointer to a heap-allocated value. [72] Similar to C++'s std::unique_ptr. | letboxed: Box<u8>=Box::new(5);letval: u8=*boxed; |
Rc<T> | Reference counting pointer [73] | letfive=Rc::new(5);letalso_five=five.clone(); |
Arc<T> | Atomic, thread-safe reference counting pointer [74] | letfoo=Arc::new(vec![1.0,2.0]);leta=foo.clone();// a can be sent to another thread |
Cell<T> | A mutable memory location [75] | letc=Cell::new(5);c.set(10); |
Mutex<T> | A mutex lock for shared data contained within. [76] | letmutex=Mutex::new(0_u32);let_guard=mutex.lock(); |
RwLock<T> | Readers–writer lock [77] | letlock=RwLock::new(5);letr1=lock.read().unwrap(); |
Condvar | A conditional monitor for shared data [78] | let(lock,cvar)=(Mutex::new(true),Condvar::new());// As long as the value inside the `Mutex<bool>` is `true`, we wait.let_guard=cvar.wait_while(lock.lock().unwrap(),|pending|{*pending}).unwrap(); |
Duration | Type that represents a span of time [79] | Duration::from_millis(1)// 1ms |
HashMap<K,V> | Hash table [80] | letmutplayer_stats=HashMap::new();player_stats.insert("damage",1);player_stats.entry("health").or_insert(100); |
BTreeMap<K,V> | B-tree [81] | letmutsolar_distance=BTreeMap::from([("Mercury",0.4),("Venus",0.7),]);solar_distance.entry("Earth").or_insert(1.0); |
Option
values are handled using syntactic sugar, such as the if let
construction, to access the inner value (in this case, a string): [82]
fnmain(){letname1: Option<&str>=None;// In this case, nothing will be printed outifletSome(name)=name1{println!("{name}");}letname2: Option<&str>=Some("Matthew");// In this case, the word "Matthew" will be printed outifletSome(name)=name2{println!("{name}");}}
Type | Description | Examples |
---|---|---|
| References (immutable and mutable) |
|
|
|
|
| A pointer to heap-allocated value (or possibly null pointer if wrapped in option) [71] |
|
|
|
|
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. [83] Option
implements a "null pointer optimization", avoiding any spatial overhead for types that cannot have a null value (references or the NonZero
types, for example). [84]
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. Unlike dereferencing, the creation of raw pointers is allowed inside of safe Rust code. [85]
Rust provides no implicit type conversion (coercion) between primitive types. But, explicit type conversion (casting) can be performed using the as
keyword. [86]
letx=1000;println!("1000 as a u16 is: {}",xasu16);
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. [87] 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. [88] With these rules, Rust can prevent the creation and use of dangling pointers: [88] [89]
fnprint_string(s: String){println!("{}",s);}fnmain(){lets=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}
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. [90]
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. [91]
Object lifetime refers to the period of time during which a reference is valid; that is, the time between the object creation and destruction. [92] These lifetimes are implicitly associated with all Rust reference types. While often inferred, they can also be indicated explicitly with named lifetime parameters (often denoted 'a
, 'b
, and so on). [93]
Lifetimes in Rust can be thought of as lexically scoped, meaning that the duration of an object lifetime is inferred from the set of locations in the source code (i.e., function, line, and column numbers) for which a variable is valid. [94] For example, a reference to a local variable has a lifetime corresponding to the block it is defined in: [94]
fnmain(){letx=5;// ------------------+- Lifetime 'a// |letr=&x;// -+-- Lifetime 'b |// | |println!("r: {}",r);// | |// | |// -+ |}// ------------------+
The borrow checker in the Rust compiler then enforces that references are only used in the locations of the source code where the associated lifetime is valid. [95] [96] In the example above, storing a reference to variable x
in 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:
fnmain(){letr;// ------------------+- Lifetime 'a// |{// |letx=5;// -+-- Lifetime 'b |r=&x;// | | // ERROR here: x does not live long enough}// -| |// |println!("r: {}",r);// |}// ------------------+
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. [97]
Lifetimes can be indicated using explicit lifetime parameters on function arguments. For example, the following code specifies that the reference returned by the function has the same lifetime as original
(and not necessarily the same lifetime as prefix
): [98]
fnremove_prefix<'a>(mutoriginal: &'astr,prefix: &str)-> &'astr{iforiginal.starts_with(prefix){original=original[prefix.len()..];}original}
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 parse_config
also showcases lifetime elision, which reduces the need for explicitly defining lifetime parameters. [99]
usestd::collections::HashMap;// This struct has one lifetime parameter, 'src. The name is only used within the struct's definition.#[derive(Debug)]structConfig<'src>{hostname: &'srcstr,username: &'srcstr,}// The '_ lifetime parameter, in this case, refers to the anonymous lifetime attached to the type// of the argument `config`.fnparse_config(config: &str)-> Config<'_>{letkey_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"],}}fnmain(){letconfig=parse_config(r#"hostname = foobarusername=barfoo"#,);println!("Parsed config: {:#?}",config);}
In the compiler, ownership and lifetimes work together to prevent memory safety issues such as dangling pointers. [100] [101]
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 [102] and is known as parametric polymorphism.
The following program calculates the sum of two things, for which addition is implemented using a generic function:
usestd::ops::Add;// sum is a generic function with one type parameter, Tfnsum<T>(num1: T,num2: T)-> TwhereT: 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}fnmain(){letresult1=sum(10,20);println!("Sum is: {}",result1);// Sum is: 30letresult2=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 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. [103]
Rust's type system supports a mechanism called traits, inspired by type classes in the Haskell language, [6] to define shared behavior between different types. For example, the Add
trait can be implemented for floats and integers, which can be added; and the Display
or Debug
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 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 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 dyn
(short for dynamic). [104] 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. [105]
In addition to defining methods for a user-defined type, the impl
keyword can be used to implement a trait for a type. [66] Traits can provide additional derived methods when implemented. [106] 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, such as map
or filter
. [107]
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, a type of polymorphism where the implementation of a polymorphic operation is chosen at runtime. This allows for behavior similar to duck typing, where all data types that implement a given trait can be treated as functionally equivalent. [108] Trait objects are declared using the syntax dyn Tr
where Tr
is a trait. Trait objects are dynamically sized, therefore they must be put behind a pointer, such as Box
. [109] The following example creates a list of objects where each object can be printed out using the Display
trait:
usestd::fmt::Display;letv: Vec<Box<dynDisplay>>=vec![Box::new(3),Box::new(5.0),Box::new("hi"),];forxinv{println!("{x}");}
If an element in the list does not implement the Display
trait, it will cause a compile-time error. [110]
Rust is designed to be memory safe. It does not permit null pointers, dangling pointers, or data races. [111] [112] [113] [114] Data values can be initialized only through a fixed set of forms, all of which require their inputs to be already initialized. [115]
Unsafe code can subvert some of these restrictions, using the unsafe
keyword. [85] Unsafe code may also be used for low-level functionality, such as volatile memory access, architecture-specific intrinsics, type punning, and inline assembly. [116]
Rust does not use garbage collection. Memory and other resources are instead managed through the "resource acquisition is initialization" convention, [117] with optional reference counting. Rust provides deterministic management of resources, with very low overhead. [118] Values are allocated on the stack by default, and all dynamic allocations must be explicit. [119]
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. [120] 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. [121]
It is possible to extend the Rust language using macros.
A declarative macro (also called a "macro by example") is a macro, defined using the macro_rules!
keyword, that uses pattern matching to determine its expansion. [122] [123] An example is the println!()
macro. [124]
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. [125] [126]
Procedural macros come in three flavors:
custom!(...)
#[derive(CustomDerive)]
#[custom_attribute]
The rsx!
macro in the Dioxus front-end framework is an example of a function-like macro. [127] [128] Theserde_derive
macro [129] 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 extendr
library for Rust bindings to R. [130]
The following code shows the use of the Serialize
, Deserialize
, and Debug
-derived procedural macros to implement JSON reading and writing, as well as the ability to format a structure for debugging.
useserde::{Serialize,Deserialize};#[derive(Serialize, Deserialize, Debug)]structPoint{x: i32,y: i32,}fnmain(){letpoint=Point{x: 1,y: 2};letserialized=serde_json::to_string(&point).unwrap();println!("serialized = {}",serialized);letdeserialized: Point=serde_json::from_str(&serialized).unwrap();println!("deserialized = {:?}",deserialized);}
Rust does not support variadic arguments in functions. Instead, it uses macros. [131]
macro_rules!calculate{// The pattern for a single `eval`(eval$e:expr)=>{{{letval: usize=$e;// Force types to be integersprintln!("{} = {}",stringify!{$e},val);}}};// Decompose multiple `eval`s recursively(eval$e:expr,$(eval$es:expr),+)=>{{calculate!{eval$e}calculate!{$(eval$es),+}}};}fnmain(){calculate!{// Look ma! Variadic `calculate!`!eval1+2,eval3+4,eval(2*3)+1}}
c_variadic
feature switch. As with other C interfaces, the system is considered unsafe
to Rust. [132] 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. As of 2024 [update] , an external library called CXX exists for calling to or from C++. [133] 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. [134]
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. [135]
The Rust compiler, rustc
, translates Rust code into low-level LLVM IR. LLVM is then invoked as a subcomponent to apply optimizations and translate the resulting IR into object code. A linker is then used to combine the objects into a single executable image or binary file. [136]
Other than LLVM, the compiler also supports using alternative backends such as GCC and Cranelift for code generation. [137] The intention of those alternative backends is to increase platform coverage of Rust or to improve compilation times. [138] [139]
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 #![no_std]
; 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 #![no_std]
. [140]
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. [141]
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. [142]
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. [143]
Clippy is Rust's built-in linting tool to improve the correctness, performance, and readability of Rust code. As of 2024 [update] , it has more than 700 rules. [144] [145]
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. [146]
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. [147]
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. [148]
In general, Rust's memory safety guarantees do not impose a runtime overhead. [149] A notable exception is array indexing which is checked at runtime, though this often does not impact performance. [150] Since it does not perform garbage collection, Rust is often faster than other memory-safe languages. [151] [90] [152]
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. [153]
Many of Rust's features are so-called zero-cost abstractions, meaning they are optimized away at compile time and incur no runtime penalty. [154] The ownership and borrowing system permits zero-copy implementations for some performance-sensitive tasks, such as parsing. [155] Static dispatch is used by default to eliminate method calls, with the exception of methods called on dynamic trait objects. [156] The compiler also uses inline expansion to eliminate function calls and statically-dispatched method invocations. [157]
Since Rust utilizes LLVM, any performance improvements in LLVM also carry over to Rust. [158] Unlike C and C++, Rust allows for reordering struct and enum elements [159] to reduce the sizes of structures in memory, for better memory alignment, and to improve cache access efficiency. [160]
Rust is used in software across different domains. Components from the Servo browser engine (funded by Mozilla and Samsung) were incorporated in the Gecko browser engine underlying Firefox. [161] In January 2023, Google (Alphabet) announced support for using third party Rust libraries in Chromium. [162] [163]
Rust is used in several backend software projects of large web services. OpenDNS, a DNS resolution service owned by Cisco, uses Rust internally. [164] [165] Amazon Web Services uses Rust in "performance-sensitive components" of its several services. In 2019, AWS open-sourced Firecracker, a virtualization solution primarily written in Rust. [166] Microsoft Azure IoT Edge, a platform used to run Azure services on IoT devices, has components implemented in Rust. [167] Microsoft also uses Rust to run containerized modules with WebAssembly and Kubernetes. [168] Cloudflare, a company providing content delivery network services, used Rust to build a new web proxy named Pingora for increased performance and efficiency. [169] The npm package manager used Rust for its production authentication service in 2019. [170] [171] [172]
In operating systems, the Rust for Linux project, launched in 2020, merged initial support into the Linux kernel version 6.1 in late 2022. [173] [174] The project is active with a team of 6-7 developers, and has added additional Rust code with kernel releases from 2022 to 2024, [175] aiming to demonstrate the minimum viability of the project and resolve key compatibility blockers. [173] [176] The first drivers written in Rust were merged into the kernel for version 6.8. [173] The Android developers used Rust in 2021 to rewrite existing components. [177] [178] Microsoft has rewritten parts of Windows in Rust. [179] The r9 project aims to re-implement Plan 9 from Bell Labs in Rust. [180] Rust has been used in the development of new operating systems such as Redox, a "Unix-like" operating system and microkernel, [181] Theseus, an experimental operating system with modular state management, [182] [183] and most of Fuchsia. [184] Rust is also used for command-line tools and operating system components, including stratisd, a file system manager [185] [186] and COSMIC, a desktop environment by System76. [187]
In web development, Deno, a secure runtime for JavaScript and TypeScript, is built on top of V8 using Rust and Tokio. [188] Other notable adoptions in this space include Ruffle, an open-source SWF emulator, [189] and Polkadot, an open source blockchain and cryptocurrency platform. [190]
Discord, an instant messaging software company, rewrote 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) used Rust to redesign its system that manages source code for internal projects. [17]
In the 2024 Stack Overflow Developer Survey, 12.6% of respondents had recently done extensive development in Rust. [191] The survey named Rust the "most admired programming language" every year from 2016 to 2024 (inclusive), based on the number of existing developers interested in continuing to work in the same language. [192] [note 7] In 2024, Rust was the 6th "most wanted technology", with 28.7% of developers not currently working in Rust expressing an interest in doing so. [191]
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 [193] [153] and performance [194] 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". [14] Likewise, an article published on Nature shared several stories of bioinformaticians using Rust for its performance and safety. [141] 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.
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. [17] [141]
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. [197] [41]
It was established on February 8, 2021, with five founding corporate members (Amazon Web Services, Huawei, Google, Microsoft, and Mozilla). [198] The foundation's board is chaired by Shane Miller. [199] Starting in late 2021, its Executive Director and CEO is Rebecca Rumbul. [200] Prior to this, Ashley Williams was interim executive director. [41]
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 as of July 2024 [update] . [201] Representatives among teams form the Leadership council, which oversees the Rust project as a whole. [202]
str
and String
are always valid UTF-8 and can contain internal zeros.In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
In object-oriented programming languages, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type, or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which type is in use. It can be thought of as a type that has several "cases", each of which should be handled correctly when that type is manipulated. This is critical in defining recursive datatypes, in which some component of a value may have the same type as that value, for example in defining a type for representing trees, where it is necessary to distinguish multi-node subtrees and leaves. Like ordinary unions, tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time.
In computer science, a union is a value that may have any of multiple representations or formats within the same area of memory; that consists of a variable that may hold such a data structure. Some programming languages support a union type for such a data type. In other words, a union type specifies the permitted types that may be stored in its instances, e.g., float
and integer
. In contrast with a record, which could be defined to contain both a float and an integer; a union would hold only one at a time.
In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.
In computing, a stack trace is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places: the stack and the heap. Memory is continuously allocated on a stack but not on a heap, thus reflective of their names. Stack also refers to a programming construct, thus to differentiate it, this stack is referred to as the program's function call stack. Technically, once a block of memory has been allocated on the stack, it cannot be easily removed as there can be other blocks of memory that were allocated before it. Each time a function is called in a program, a block of memory called an activation record is allocated on top of the call stack. Generally, the activation record stores the function's arguments and local variables. What exactly it contains and how it's laid out is determined by the calling convention.
In computing, a channel is a model for interprocess communication and synchronization via message passing. A message may be sent over a channel, and another process or thread is able to receive messages sent over a channel it has a reference to, as a stream. Different implementations of channels may be buffered or not, and either synchronous or asynchronous.
In computer programming, variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope. At the level of identifiers, this is known as name masking. This outer variable is said to be shadowed by the inner variable, while the inner identifier is said to mask the outer identifier. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.
In programming languages and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional value; e.g., it is used as the return type of functions which may or may not return a meaningful value when they are applied. It consists of a constructor which either is empty, or which encapsulates the original data type A
.
Different command-line argument parsing methods are used by different programming languages to parse command-line arguments.
In computer programming, string interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of quasi-quotation. The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context.
In functional programming, a result type is a monadic type holding a returned value or an error code. They provide an elegant way of handling errors, without resorting to exception handling; when a function that may fail returns a result type, the programmer is forced to consider success or failure paths, before getting access to the expected result; this eliminates the possibility of an erroneous programmer assumption.
In computing, static dispatch is a form of polymorphism fully resolved during compile time. It is a form of method dispatch, which describes how a language or environment will select which implementation of a method or function to use.
In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, and is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C#, C++, Python, F#, Hack, Julia, Dart, Kotlin, Rust, Nim, JavaScript, and Swift.
Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level system programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.
Zig is an imperative, general-purpose, statically typed, compiled system programming language designed by Andrew Kelley. It is intended as a successor to the language C, with the intent of being even smaller and simpler to program in, while offering more functionality. It is free and open-source software, released under an MIT License.
Monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation. It is considered beneficial to undergo the mentioned transformation because it results in the output intermediate representation (IR) having specific types, which allows for more effective optimization. Additionally, many IRs are intended to be low-level and do not accommodate polymorphism. The resulting code is generally faster than dynamic dispatch, but may require more compilation time and storage space due to duplicating the function body.
Tokio is a software library for the Rust programming language. It provides a runtime and functions that enable the use of asynchronous I/O, allowing for concurrency in regards to task completion.
A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler. The data type should preferably be immutable if possible. It is common for implementations to handle equality testing, serialization and model binding.
Those of you familiar with the Elm style may recognize that the updated --explain messages draw heavy inspiration from the Elm approach.
They are inspired by ... ownership types and borrowed pointers in the Rust programming language.
Both are curly bracket languages, with C-like syntax that makes them unintimidating for C programmers.
We observe a large variance in the overheads of checked indexing: 23.6% of benchmarks do report significant performance hits from checked indexing, but 64.5% report little-to-no impact and, surprisingly, 11.8% report improved performance ... Ultimately, while unchecked indexing can improve performance, most of the time it does not.
... While some compilers (e.g., Rust) support structure reordering [82], C & C++ compilers are forbidden to reorder data structures (e.g., struct or class) [74] ...