This is a good article. Click here for more information.

Rust (programming language)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
Rust
A capitalized letter R set into a sprocket
The official Rust logo
Paradigms <templatestyles src="Cslist/styles.css" />
Designed by Graydon Hoare
First appeared May 15, 2015; 8 years ago (2015-05-15)
Stable release Lua error in Module:Wd at line 405: invalid escape sequence near '"^'. / Lua error in Module:Wd at line 405: invalid escape sequence near '"^'.; Error: first parameter cannot be parsed as a date or time. (Lua error in Module:Wd at line 405: invalid escape sequence near '"^'.)
Typing discipline <templatestyles src="Cslist/styles.css" />
Implementation language Rust
Platform Cross-platform[note 1]
OS Cross-platform[note 2]
License MIT and Apache 2.0[note 3]
Filename extensions .rs, .rlib
Website {{#property:P856}}
Influenced by
<templatestyles src="Cslist/styles.css" />
Influenced
<templatestyles src="Cslist/styles.css" />

Rust is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory, without requiring the use of automated memory management techniques such as garbage collection. 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.[11][12][13]

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, Facebook (Meta), Google (Alphabet), 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,[14] and has been studied in programming language theory research.[15][16][17]

History

Origins (2006–2012)

Rust grew out of a personal project begun in 2006 by Mozilla Research employee Graydon Hoare.[18] Mozilla began sponsoring the project in 2009 as a part of the ongoing development of an experimental browser engine called Servo,[19] which was officially announced by Mozilla in 2010.[20][21] During the same year, 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.[19]

Hoare later said that Rust was named after the rust fungus, with reference to the fungus's hardiness.[18][22]

Evolution (2012–2019)

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.[23] Four months later, version 0.3 added destructors and polymorphism, through the use of interfaces.[24] 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.[25]

Through 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.[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.[26] The first stable release, Rust 1.0, was announced on May 15, 2015.[27][28]

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".[29]

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.[30][31] 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.[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 its five founding companies (AWS, Huawei, Google, Microsoft, and Mozilla).[34][35] 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][37]

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".[38] 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.[39]

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.[40]

Syntax and features

Rust's syntax is similar to that of C and C++,[41][42] although many of its features were influenced by functional programming languages.[43] Hoare 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.

Hello World program

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

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

fn main() {
    println!("Hello, World!");
}

Keywords and control flow

In Rust, blocks of code are delimited by curly brackets, and control flow is implemented by keywords such as if, else, while, and for.[45] Pattern matching can be done using the match keyword.[46] In the examples below, explanations are given in comments, which start with //.[47]

fn main() {
    // Defining a mutable variable with 'let mut'
    // Using the macro vec! to create a vector
    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"),
        // pattern matching can use ranges of integers
        2..=10 => println!("Between two and ten values"),
        11 => println!("Eleven values"),
        // A `_` pattern is called a "wildcard", it matches any value
        _ => 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
    }
}

Expression blocks

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

Rust is expression-oriented, with nearly every part of a function body being an expression, including control-flow operators.[48] The ordinary if expression is used instead of C's ternary conditional. 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,[49] 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()
}

Closures

Lua error in Module:TNT at line 159: Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:I18n/Module:Excerpt.tab.

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. Variables are declared with the keyword let, and type inference is used to determine their type.[50] Variables assigned multiple times must be marked with the keyword mut (short for mutable).[51]

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.[52]

Primitive types

Summary of Rust's Primitive Types
Type Description Examples
bool Boolean value <templatestyles src="Plainlist/styles.css"/>
  • true
  • false
u8 Unsigned 8-bit integer (a byte) <templatestyles src="Plainlist/styles.css"/>
<templatestyles src="Plainlist/styles.css"/>
  • i8
  • i16
  • i32
  • i64
  • i128
Signed integers, up to 128 bits <templatestyles src="Plainlist/styles.css"/>
<templatestyles src="Plainlist/styles.css"/>
  • u16
  • u32
  • u64
  • u128
Unsigned integers, up to 128 bits <templatestyles src="Plainlist/styles.css"/>
<templatestyles src="Plainlist/styles.css"/>
  • usize
  • isize
Pointer-sized integers (size depends on platform) <templatestyles src="Plainlist/styles.css"/>
<templatestyles src="Plainlist/styles.css"/>
  • f32
  • f64
Floating-point numbers <templatestyles src="Plainlist/styles.css"/>
char <templatestyles src="Plainlist/styles.css"/>
<templatestyles src="Plainlist/styles.css"/>
str UTF-8-encoded‘string slice’, is the most primitive string type. It is usually seen in its borrowed form, &str. It is also the type of string literals, &'static str[54] <templatestyles src="Plainlist/styles.css"/>
  • "Hello"
  • "3"
  • "🦀🦀🦀"
[T; N] Array – collection of N objects of the same type T, stored in contiguous memory <templatestyles src="Plainlist/styles.css"/>
  • [2, 4, 6]
  • [0; 100]
  • b"Hello"
[T] Slice – a dynamically-sized view into a contiguous sequence[55] <templatestyles src="Plainlist/styles.css"/>
  • [1, 2, 3, 4, 5][..i]
  • "Hello, world!".as_bytes()
  • let v = vec![1, 2, 3]; v.as_slice()
<templatestyles src="Plainlist/styles.css"/>
(T, U, ..)
Tuple – a finite heterogeneous sequence <templatestyles src="Plainlist/styles.css"/>
  • () (An empty tuple, the unit type in Rust)
  • (5,) ((5) is parsed as an integer)[56]
  • ("Age", 10)
  • (1, true, "Name")
! Never type (unreachable value) let x = { return 123 };

Standard library

Summary of Rust's types in the standard library
Type Description Examples
Box<T> A pointer to a heap-allocated value[57]
let boxed: Box<u8> = Box::new(5);
let val: u8 = *boxed;
String UTF-8-encoded strings (dynamic) <templatestyles src="Plainlist/styles.css"/>
  • String::new()
  • String::from("Hello")
  • "🦀🦀🦀".to_string()
<templatestyles src="Plainlist/styles.css"/>
  • OsStr
  • OsString
Platform-native strings[note 7] (borrowed[58] and dynamic[59]) <templatestyles src="Plainlist/styles.css"/>
  • OsStr::new("Hello")
  • OsString::from("world")
<templatestyles src="Plainlist/styles.css"/>
  • Path
  • PathBuf
Paths (borrowed[60] and dynamic[61]) <templatestyles src="Plainlist/styles.css"/>
  • Path::new("./path/to")
  • PathBuf::from(r"C:.\path\to")
<templatestyles src="Plainlist/styles.css"/>
  • CStr
  • CString
C-compatible, null-terminated strings (borrowed[62] and dynamic[63]) <templatestyles src="Plainlist/styles.css"/>
  • CStr::from_bytes_with_nul(b"Hello\0").unwrap()
  • CString::new("world").unwrap()
Vec<T> Dynamic arrays <templatestyles src="Plainlist/styles.css"/>
  • Vec::new()
  • vec![1, 2, 3, 4, 5]
Option<T> Option type <templatestyles src="Plainlist/styles.css"/>
  • None
  • Some(3)
  • Some("hello")
Result<T, E> Error handling <templatestyles src="Plainlist/styles.css"/>
  • Result::Ok(3)
  • Result::Err("something went wrong")
Rc<T> Reference counting pointer[64]
let five = Rc::new(5);
let also_five = five.clone();
Arc<T> Thread-safe reference counting pointer[65]
let foo = Arc::new(vec![1.0, 2.0]);
let a = foo.clone(); // a can be sent to another thread
Cell<T> A mutable memory location[66]
let c = Cell::new(5);
c.set(10);
Mutex<T> A mutex lock for shared data[67]
let mutex = Mutex::new(0_u32);
let _guard = mutex.lock();
RwLock<T> Readers–writer lock[68]
let lock = RwLock::new(5);
let r1 = lock.read().unwrap();
Condvar A conditional monitor for shared data[69]
 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[70]
Duration::from_millis(1) // 1ms
HashMap<K, V> Hash table[71]
let mut player_stats = HashMap::new();
player_stats.insert("damage", 1);
player_stats.entry("health").or_insert(100);
BTreeMap<K, V> B-tree[72]
let mut solar_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):[73]

fn main() {
    let name1: Option<&str> = None;
    // In this case, nothing will be printed out
    if let Some(name) = name1 {
        println!("{name}");
    }

    let name2: Option<&str> = Some("Matthew");
    // In this case, the word "Matthew" will be printed out
    if let Some(name) = name2 {
        println!("{name}");
    }
}

Pointers

Summary of Rust's pointer and reference primitive types
Type Description Examples
<templatestyles src="Plainlist/styles.css"/>
  • &T
  • &mut T
References (immutable and mutable) <templatestyles src="Plainlist/styles.css"/>
  • let x_ref = &x;
  • let x_ref = &mut x;
<templatestyles src="Plainlist/styles.css"/>
  • Option<&T>
  • Option<&mut T>
<templatestyles src="Plainlist/styles.css"/>
  • Option wrapped reference
  • Possibly null reference
<templatestyles src="Plainlist/styles.css"/>
  • None
  • let x_ref = Some(&x);
  • let x_ref = Some(&mut x);
<templatestyles src="Plainlist/styles.css"/>
  • Box<T>
  • Option<Box<T>>
A pointer to heap-allocated value

(or possibly null pointer if wrapped in option)[74]

<templatestyles src="Plainlist/styles.css"/>
  • let boxed = Box::new(0);
  • let boxed = Some(Box::new("Hello World"));
<templatestyles src="Plainlist/styles.css"/>
  • *const T
  • *mut T
<templatestyles src="Plainlist/styles.css"/>
  • Raw pointers (immutable and mutable)
  • Possibly null; unsafe to dereference
<templatestyles src="Plainlist/styles.css"/>
  • let x_ptr = &x as *const T;
  • let x_ptr = &mut x as *mut T;

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.[75] Option implements a "null pointer optimization", avoiding any overhead for types that 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. Unlike dereferencing, the creation of raw pointers is allowed inside of safe Rust code.[76]

User-defined types

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.[77] enums can take on different variants in runtime, with its capabilities similar to algebraic data types found in functional programming languages.[78] Both structs and enums can contain fields with different types.[79] Alternative names for the same type can be defined with the type keyword.[80]

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.[81]

Ownership and lifetimes

Rust's ownership system consists of rules that ensure memory safety without using a garbage collector. At run time, each value must be attached to a variable called the owner of that value, and every value must have exactly one owner.[82] 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.[83] With these rules, Rust can prevent the creation and use of dangling pointers:[83][84]

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
}

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.[85]

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. The borrow checker in the Rust compiler uses lifetimes to ensure that the values a reference points to remain valid. It also ensures that a mutable reference exists only if no immutable references exist at the same time.[86][87] 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 created and used by functions, using lifetime parameters, as a signature feature.[88]

When a stack or temporary variable goes out of scope, it is dropped by running its destructor. The destructor may be programmatically defined through the drop function. This technique enforces the so-called resource acquisition is initialization (RAII) design pattern, in which resources, such as file descriptors or network sockets, are tied to the lifetime of an object: when the object is dropped, the resource is closed.[89][90]

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 must 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.[91]

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);
}
File:Rust 101.webm
A presentation on Rust by Emily Dunham from Mozilla's Rust team (linux.conf.au conference, Hobart, 2017)

Memory safety

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

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

Memory management

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

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.[100] 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.[101]

Polymorphism

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[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:

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 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]

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 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.[81] 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]

Traits follow the composition over inheritance design principle.[108] That is, traits cannot define fields themselves; they provide a restricted form of inheritance where methods can be defined and mixed in to implementations.

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).[109] Dynamically dispatched 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.[110] The following example creates a list of objects where each object can be printed out using the Display trait:

use std::fmt::Display;

let v: Vec<Box<dyn Display>> = vec![
    Box::new(3),
    Box::new(5.0),
    Box::new("hi"),
];

for x in v {
    println!("{x}");
}

If an element in the list does not implement the Display trait, it will cause a compile time error.[111]

Iterators

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

for x in 0..100 {
   f(x);
}

0..100 is a value of type Range which implements the Iterator trait; the code applies the function f 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()

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.[112][113]

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.[114][115]

Procedural macros come in three flavors:

  • Function-like macros custom!(...)
  • Derive macros #[derive(CustomDerive)]
  • Attribute macros #[custom_attribute]

The println! macro is an example of a function-like macro. Theserde_derive macro[116] 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.[117]

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.

File:Rust serde UML diagram.svg
A UML diagram depicting a Rust struct named Point.
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);
}

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. Rust also has a library, CXX, for calling to or from C++.[118] 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.[119]

Components

File:Rustc building paru with cargo screenshot.png
Compiling a Rust program with Cargo

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.[120]

Compiler

The Rust compiler is named rustc. Internally, rustc is a frontend to various backends that are used for further device-specific and platform-specific binary code files (e.g. ELF or WASM binary) generation (compilation). The most mature and most used backend in rustc is the LLVM intermediate representation (bytecode) compiler, but a Cranelift-based backend and a GCC-based backend are also available in nightly builds of the compiler.[121][122]

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 #![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].[123]

File:Crates.io website.png
Screenshot of crates.io in June 2022

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.[14]

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.[124]

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.[125]

File:Cargo clippy hello world example.png
Example output of Clippy on a hello world Rust program

Clippy

Clippy is Rust's built-in linting tool to improve the correctness, performance, and readability of Rust code. It was created in 2014[126][better source needed] and named after Microsoft Office's assistant, an anthropomorphized paperclip of the same name.[127] As of 2021, it has more than 450 rules,[128][better source needed] which can be browsed online and filtered by category.[129][130]

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.[131]

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.[132]

IDE support

The most popular language server for Rust is Rust Analyzer, which officially replaced the original language server, RLS, in July 2022.[133] Rust Analyzer provides IDEs and text editors with information about a Rust project; basic features including autocompletion, and the display of compilation errors while editing.[134]

Performance

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

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 useful for cases where the compiler is too restrictive.[140]

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

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

Adoption

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

File:Home page servo v0.01.png
Early homepage of Mozilla's Servo browser engine

Rust has been used in software spanning across different domains. Rust was initially funded by Mozilla as part of developing Servo, an experimental parallel browser engine, in collaboration with Samsung.[148] Components from the Servo engine were later incorporated in the Gecko browser engine underlying Firefox.[149]

Rust is used in several backend software projects of large web services. OpenDNS, a DNS resolution service owned by Cisco, uses Rust internally.[150][151] Cloudflare, a company providing content delivery network services uses Rust for its firewall pattern matching engine.[152][153] Discord, an instant messaging social platform uses Rust for portions of its backend, as well as client-side video encoding.[154] In 2021, Dropbox announced their use of Rust for a screen, video, and image capturing service.[155] Facebook (Meta) used Rust for Mononoke, a server for the Mercurial version control system.[156] Google (Alphabet) announced support for Rust in the Android operating system also in 2021.[36][157]

Amazon Web Services began developing projects in Rust as early as 2017,[158] including Firecracker, a virtualization solution;[159] Bottlerocket, a Linux distribution and containerization solution;[160] and Tokio, an asynchronous networking stack.[161] Microsoft Azure IoT Edge, a platform used to run Azure services on IoT devices, has components implemented in Rust.[162] Microsoft also uses Rust to run containerized modules with WebAssembly and Kubernetes.[163]

In operating systems, the Rust for Linux project was begun in 2021 to add Rust support to the Linux kernel.[164] Support for Rust (along with support for C and Assembly language) was officially added in version 6.1.[165] Redox is a "Unix-like operating system" that includes a microkernel written in Rust.[166] Another operating system named Theseus is an experiment in operating system design using Rust to enforce modular state management between operating system components.[167][168] Rust is also used for command-line tools and specific operating system components, including stratisd, a file system manager[169][170] and COSMIC, a desktop environment by System76.[171][172]

Microsoft announced in 2020 that parts of Microsoft Windows are being rewritten in Rust. As of 2023, DWriteCore, a system library for text layout and glyph render, has about 152,000 lines of Rust code and about 96,000 lines of C++ code, and saw a performance increase of 5 to 15 percent in some cases.[173]

File:Ruffle.rs demo (1).png
Ruffle, a web emulator for Adobe Flash SWF files

In web development, Deno, a secure runtime for JavaScript and TypeScript, is built with V8, Rust, and Tokio.[174] Ruffle is an open-source SWF emulator written in Rust.[175] Other notable projects include TerminusDB, an open source distributed graph database for knowledge graphs,[176][better source needed] and Polkadot, an open source blockchain platform and cryptocurrency.[177]

In the 2023 Stack Overflow Developer Survey, 13% of respondents had recently done extensive development in Rust.[178] 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.[179][note 8] In 2023, Rust was the 6th "most wanted technology", with 31% of developers not currently working in Rust expressing an interest in doing so.[178]

Community

File:Rustacean-orig-noshadow.svg
Some Rust users refer to themselves as Rustaceans (a pun on crustacean) and have adopted an orange crab, Ferris, as their unofficial mascot.[180]

Conferences

Rust's official website lists online forums, messaging platforms, and in-person meetups for the Rust community.[181] Conferences dedicated to Rust development in the past have included:

  • RustConf, held annually in Portland, Oregon since 2016 (except in 2020 and 2021 because of the COVID-19 pandemic).[182]
  • RustFest Global, a Rust conference with online and local components[183] previously known as RustFest in Europe.[184]
  • Rust Belt Rust, a Rust conference in the United States Rust Belt in 2019.[185]
  • RustCon Asia, held in Beijing in 2019.[186]
  • Rust LATAM, to be held in Mexico city in 2020, but canceled due to COVID-19.[187]
  • Oxidize Global, an online conference held in 2020.[188]

Rust Foundation

Rust Foundation
250px
Formation February 8, 2021; 3 years ago (2021-02-08)
Founders
Type Nonprofit organization
Location
Shane Miller
Rebecca Rumbul
Website foundation.rust-lang.org

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.[189][42]

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

Governance teams

The Rust project is composed of teams that are responsible for different subareas of the development. For example, the Core team manages Rust's overall direction, supervises subteams, and deals with cross-cutting issues; the compiler team develops, manages, and optimizes compiler internals; and the language team is in charge of designing and helping to implement new language features.[194][195]

See also

Notes

  1. Including build tools, host tools, and standard library support for x86-64, ARM, MIPS, RISC-V, WebAssembly, i686, AArch64, PowerPC, and s390x.[1]
  2. Including Windows, Linux, macOS, FreeBSD, NetBSD, and Illumos. Host build tools on Android, iOS, Haiku, Redox, and Fuchsia are not officially shipped; these operating systems are supported as targets.[1]
  3. Third-party dependencies, e.g., LLVM or MSVC, are subject to their own licenses.[2][3]
  4. 4.0 4.1 4.2 4.3 4.4 4.5 This literal uses an explicit suffix, which is not needed when type can be inferred from the context
  5. Interpreted as i32 by default, or inferred from the context
  6. Type inferred from the context
  7. On Unix systems, this is often UTF-8 strings without an internal 0 byte. On Windows, this is UTF-16 strings without an internal 0 byte. Unlike these, str and String are always valid UTF-8 and can contain internal zeros.
  8. That is, among respondents who have done "extensive development work [with Rust] in over the past year" (13.05%), Rust had the largest percentage who also expressed interest to "work in [Rust] over the next year" (84.66%).[178]

References

Book sources

  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.

Others

  1. 1.0 1.1 Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. 5.0 5.1 Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. 14.0 14.1 Lua error in package.lua at line 80: module 'strict' not found.
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. 18.0 18.1 18.2 Lua error in package.lua at line 80: module 'strict' not found.
  18. 19.0 19.1 19.2 Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. Lua error in package.lua at line 80: module 'strict' not found.
  23. Lua error in package.lua at line 80: module 'strict' not found.
  24. Lua error in package.lua at line 80: module 'strict' not found.
  25. Lua error in package.lua at line 80: module 'strict' not found.
  26. Lua error in package.lua at line 80: module 'strict' not found.
  27. Lua error in package.lua at line 80: module 'strict' not found.
  28. Lua error in package.lua at line 80: module 'strict' not found.
  29. Lua error in package.lua at line 80: module 'strict' not found.
  30. Lua error in package.lua at line 80: module 'strict' not found.
  31. Lua error in package.lua at line 80: module 'strict' not found.
  32. Lua error in package.lua at line 80: module 'strict' not found.
  33. Lua error in package.lua at line 80: module 'strict' not found.
  34. Lua error in package.lua at line 80: module 'strict' not found.
  35. 36.0 36.1 Lua error in package.lua at line 80: module 'strict' not found.
  36. Lua error in package.lua at line 80: module 'strict' not found.
  37. Lua error in package.lua at line 80: module 'strict' not found.
  38. Lua error in package.lua at line 80: module 'strict' not found.
  39. Lua error in package.lua at line 80: module 'strict' not found.
  40. Lua error in package.lua at line 80: module 'strict' not found.
  41. 42.0 42.1 Lua error in package.lua at line 80: module 'strict' not found.
  42. Klabnik & Nichols 2019, p. 263.
  43. Klabnik & Nichols 2019, pp. 5–6.
  44. Klabnik & Nichols 2019, pp. 49–57.
  45. Klabnik & Nichols 2019, pp. 104–109.
  46. Klabnik & Nichols 2019, p. 49.
  47. Klabnik & Nichols 2019, pp. 50–53.
  48. Lua error in package.lua at line 80: module 'strict' not found.
  49. Klabnik & Nichols 2019, pp. 24.
  50. Klabnik & Nichols 2019, pp. 32–33.
  51. Klabnik & Nichols 2019, pp. 36–38.
  52. Klabnik & Nichols 2019, pp. 39–40.
  53. Lua error in package.lua at line 80: module 'strict' not found.
  54. Lua error in package.lua at line 80: module 'strict' not found.
  55. Lua error in package.lua at line 80: module 'strict' not found.
  56. Lua error in package.lua at line 80: module 'strict' not found.
  57. Lua error in package.lua at line 80: module 'strict' not found.
  58. Lua error in package.lua at line 80: module 'strict' not found.
  59. Lua error in package.lua at line 80: module 'strict' not found.
  60. Lua error in package.lua at line 80: module 'strict' not found.
  61. Lua error in package.lua at line 80: module 'strict' not found.
  62. Lua error in package.lua at line 80: module 'strict' not found.
  63. Lua error in package.lua at line 80: module 'strict' not found.
  64. Lua error in package.lua at line 80: module 'strict' not found.
  65. Lua error in package.lua at line 80: module 'strict' not found.
  66. Lua error in package.lua at line 80: module 'strict' not found.
  67. Lua error in package.lua at line 80: module 'strict' not found.
  68. Lua error in package.lua at line 80: module 'strict' not found.
  69. Lua error in package.lua at line 80: module 'strict' not found.
  70. Lua error in package.lua at line 80: module 'strict' not found.
  71. Lua error in package.lua at line 80: module 'strict' not found.
  72. McNamara 2021.
  73. Lua error in package.lua at line 80: module 'strict' not found.
  74. Klabnik & Nichols 2019, pp. 101–104.
  75. 76.0 76.1 Klabnik & Nichols 2019, pp. 418–427.
  76. Klabnik & Nichols 2019, p. 83.
  77. Klabnik & Nichols 2019, p. 97.
  78. Klabnik & Nichols 2019, pp. 98–101.
  79. Klabnik & Nichols 2019, pp. 438–440.
  80. 81.0 81.1 Klabnik & Nichols 2019, pp. 93.
  81. Klabnik & Nichols 2019, pp. 59–61.
  82. 83.0 83.1 Klabnik & Nichols 2019, pp. 63–68.
  83. Klabnik & Nichols 2019, pp. 74–75.
  84. Lua error in package.lua at line 80: module 'strict' not found.
  85. Klabnik & Nichols 2019, pp. 75,134.
  86. Lua error in package.lua at line 80: module 'strict' not found.
  87. Klabnik & Nichols 2019, pp. 192–204.
  88. Lua error in package.lua at line 80: module 'strict' not found.
  89. Lua error in package.lua at line 80: module 'strict' not found.
  90. Klabnik & Nichols 2019, pp. 201–203.
  91. Lua error in package.lua at line 80: module 'strict' not found.
  92. Lua error in package.lua at line 80: module 'strict' not found.
  93. Lua error in package.lua at line 80: module 'strict' not found.
  94. Lua error in package.lua at line 80: module 'strict' not found.
  95. McNamara 2021, p. 139, 376–379, 395.
  96. Lua error in package.lua at line 80: module 'strict' not found.
  97. Lua error in package.lua at line 80: module 'strict' not found.
  98. Lua error in package.lua at line 80: module 'strict' not found.
  99. Klabnik & Nichols 2019, pp. 70–75.
  100. Klabnik & Nichols 2019, p. 323.
  101. Klabnik & Nichols 2019, pp. 171–172.
  102. Klabnik & Nichols 2019, pp. 171–172,205.
  103. Klabnik & Nichols 2019, pp. 181,182.
  104. Gjengset 2021, p. 25.
  105. Klabnik & Nichols 2019, pp. 182–184.
  106. Klabnik & Nichols 2019, pp. 281–283.
  107. Lua error in package.lua at line 80: module 'strict' not found.
  108. Lua error in package.lua at line 80: module 'strict' not found.
  109. Klabnik & Nichols 2019, pp. 441–442.
  110. Klabnik & Nichols 2019, pp. 379–380.
  111. Lua error in package.lua at line 80: module 'strict' not found.
  112. Klabnik & Nichols 2019, pp. 446–448.
  113. Lua error in package.lua at line 80: module 'strict' not found.
  114. Klabnik & Nichols 2019, pp. 449–455.
  115. Lua error in package.lua at line 80: module 'strict' not found.
  116. Lua error in package.lua at line 80: module 'strict' not found.
  117. Lua error in package.lua at line 80: module 'strict' not found.
  118. Lua error in package.lua at line 80: module 'strict' not found.
  119. Lua error in package.lua at line 80: module 'strict' not found.
  120. Lua error in package.lua at line 80: module 'strict' not found.
  121. Lua error in package.lua at line 80: module 'strict' not found.
  122. Gjengset 2021, p. 213-215.
  123. Lua error in package.lua at line 80: module 'strict' not found.
  124. Klabnik & Nichols 2019, pp. 511–512.
  125. Lua error in package.lua at line 80: module 'strict' not found.
  126. Lua error in package.lua at line 80: module 'strict' not found.
  127. Lua error in package.lua at line 80: module 'strict' not found.
  128. Lua error in package.lua at line 80: module 'strict' not found.
  129. Klabnik & Nichols 2019, pp. 513–514.
  130. Klabnik & Nichols 2019, Appendix G – How Rust is Made and "Nightly Rust"
  131. Blandy, Orendorff & Tindall 2021, pp. 176–177.
  132. Lua error in package.lua at line 80: module 'strict' not found.
  133. Klabnik & Nichols 2023, p. 623.
  134. McNamara 2021, p. 11.
  135. Lua error in package.lua at line 80: module 'strict' not found.
  136. Lua error in package.lua at line 80: module 'strict' not found.
  137. Lua error in package.lua at line 80: module 'strict' not found.
  138. Lua error in package.lua at line 80: module 'strict' not found.
  139. Lua error in package.lua at line 80: module 'strict' not found.
  140. McNamara 2021, p. 19, 27.
  141. Lua error in package.lua at line 80: module 'strict' not found.
  142. McNamara 2021, p. 20.
  143. Lua error in package.lua at line 80: module 'strict' not found.
  144. Lua error in package.lua at line 80: module 'strict' not found.
  145. Lua error in package.lua at line 80: module 'strict' not found.
  146. Lua error in package.lua at line 80: module 'strict' not found.
  147. Lua error in package.lua at line 80: module 'strict' not found.
  148. Lua error in package.lua at line 80: module 'strict' not found.
  149. Lua error in package.lua at line 80: module 'strict' not found.
  150. Lua error in package.lua at line 80: module 'strict' not found.
  151. Lua error in package.lua at line 80: module 'strict' not found.
  152. Lua error in package.lua at line 80: module 'strict' not found.
  153. Lua error in package.lua at line 80: module 'strict' not found.
  154. Lua error in package.lua at line 80: module 'strict' not found.
  155. Lua error in package.lua at line 80: module 'strict' not found.
  156. Lua error in package.lua at line 80: module 'strict' not found.
  157. Lua error in package.lua at line 80: module 'strict' not found.
  158. Lua error in package.lua at line 80: module 'strict' not found.
  159. Lua error in package.lua at line 80: module 'strict' not found.
  160. Lua error in package.lua at line 80: module 'strict' not found.
  161. Lua error in package.lua at line 80: module 'strict' not found.
  162. Lua error in package.lua at line 80: module 'strict' not found.
  163. Lua error in package.lua at line 80: module 'strict' not found.
  164. Lua error in package.lua at line 80: module 'strict' not found.
  165. Lua error in package.lua at line 80: module 'strict' not found.
  166. Lua error in package.lua at line 80: module 'strict' not found.
  167. Lua error in package.lua at line 80: module 'strict' not found.
  168. Lua error in package.lua at line 80: module 'strict' not found.
  169. Lua error in package.lua at line 80: module 'strict' not found.
  170. Lua error in package.lua at line 80: module 'strict' not found.
  171. Lua error in package.lua at line 80: module 'strict' not found.
  172. Lua error in package.lua at line 80: module 'strict' not found.
  173. Lua error in package.lua at line 80: module 'strict' not found.
  174. Lua error in package.lua at line 80: module 'strict' not found.
  175. Lua error in package.lua at line 80: module 'strict' not found.
  176. Lua error in package.lua at line 80: module 'strict' not found.
  177. 178.0 178.1 178.2 Lua error in package.lua at line 80: module 'strict' not found.
  178. Lua error in package.lua at line 80: module 'strict' not found.
  179. Lua error in package.lua at line 80: module 'strict' not found.
  180. Lua error in package.lua at line 80: module 'strict' not found.
  181. Lua error in package.lua at line 80: module 'strict' not found.
  182. Lua error in package.lua at line 80: module 'strict' not found.
  183. Lua error in package.lua at line 80: module 'strict' not found.
  184. Lua error in package.lua at line 80: module 'strict' not found.
  185. Lua error in package.lua at line 80: module 'strict' not found.
  186. Lua error in package.lua at line 80: module 'strict' not found.
  187. Lua error in package.lua at line 80: module 'strict' not found.
  188. Lua error in package.lua at line 80: module 'strict' not found.
  189. Lua error in package.lua at line 80: module 'strict' not found.
  190. Lua error in package.lua at line 80: module 'strict' not found.
  191. Lua error in package.lua at line 80: module 'strict' not found.
  192. Lua error in package.lua at line 80: module 'strict' not found.
  193. Lua error in package.lua at line 80: module 'strict' not found.
  194. Lua error in package.lua at line 80: module 'strict' not found.

Further reading

  • Lua error in package.lua at line 80: module 'strict' not found.

External links

  • No URL found. Please specify a URL here or add one to Wikidata.