Rust (programming language): Difference between revisions

Content deleted Content added
Pointers: using syntax highlighted snippets where possible; c/e
enums in rust are actually tagged unions
Line 70:
'''Rust''' is a [[General-purpose programming language|general-purpose]] [[programming language]] emphasizing [[Computer performance|performance]], [[type safety]], and [[Concurrency (computer science)|concurrency]]. It enforces [[memory safety]], meaning that all [[Reference (computer science)|references]] point to valid memory. It does so without a conventional [[Garbage collection (computer science)|garbage collector]]; instead, memory safety errors and [[data race]]s are prevented by the "borrow checker", which tracks the [[object lifetime]] of references [[Compiler|at compile time]].
 
Rust supports multiple [[programming paradigm]]s. It was influenced by ideas from [[functional programming]], including [[Immutable object|immutability]], [[higher-order function]]s, [[algebraic data type]]s, and [[pattern matching]]. It also supports [[object-oriented programming]] via structs, [[EnumeratedUnion type|enums]], traits, and methods.
 
Software developer Graydon Hoare created Rust as a personal project while working at [[Mozilla]] Research in 2006. Mozilla officially sponsored the project in 2009. The first stable release of Rust, Rust 1.0, was published in May 2015. Following a large layoff of Mozilla employees in August 2020, multiple other companies joined Mozilla in sponsoring Rust through the creation of the [[#Rust Foundation|Rust Foundation]] in February 2021. In December 2022, Rust became the first language other than [[C (programming language)|C]] and [[Assembly language|assembly]] to be supported in the development of the [[Linux kernel]].
Line 452:
==== Standard library ====
 
The Rust [[standard library]] defines and implements many widely used custom data types, including core data structures such as {{code|Vec}}, {{code|Option}}, and {{code|HashMap}}, as well as [[smart pointer]] types. Rust also provides a way to exclude most of the standard library using the attribute {{rust|#![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, {{code|core}}, {{code|alloc}}, and {{code|std}}, where {{code|std}} and {{Code|alloc}} are excluded by {{rust|#![no_std]}}.{{sfn|Gjengset|2021|pp=213-215}}
 
Rust uses [[Option type|<code>Option</code>]] tovalues defineare optionalhandled values,using which[[syntactic cansugar]], besuch matchedas usingthe <code>if let</code> or <code>match</code>construction, to access the inner value (in this case, a string):{{sfn|KlabnikMcNamara|Nichols|2023|pp=108-110,113-114,116-1172021}}
 
<syntaxhighlight lang="rust">
Line 473:
 
=== Pointers ===
{| class="wikitable"
The <code>&</code> and {{rust|&mut}} reference types are guaranteed to not be null and points to valid memory.{{sfn|Gjengset|2021|p=155-156}} The raw pointer types {{rust|*const}} and {{rust|*mut}} opts out of the safety guarantees, thus they may be null and invalid; however, it is impossible to dereference them unless the code is explicitly declared unsafe through the use of an {{rust|unsafe}} block.{{sfn|Klabnik|Nichols|2023|pp=421-423}} Unlike dereferencing, the creation of raw pointers is allowed inside safe Rust code.{{sfn|Klabnik|Nichols|2019|pp=418–427}}
|+Summary of Rust's [[Pointer (computer programming)|pointer]] and [[Reference (computer science)|reference]] primitive types
!Type
!Description
!Examples
|-
|{{plainlist|
* {{Rust|&T}}
* {{Rust|&mut T}}
}}
|[[Reference (computer science)|References]] (immutable and mutable)
|{{plainlist|
* {{Rust|let x_ref: &T {{=}} {{not a typo|&x;}}}}
* {{Rust|let x_ref: &mut T {{=}} &mut x;}}
}}
|-
|{{plainlist|
* {{Rust|Option<&T>}}
* {{Rust|Option<&mut T>}}
}}
|{{plainlist|
* Option wrapped reference
* Possibly null reference
}}
|{{plainlist|
* {{Rust|None}}
* {{Rust|let x_ref: Option<&T> {{=}} Some(&x);}}
* {{Rust|let x_ref: Option<&mut T> {{=}} Some(&mut x);}}
}}
|-
|{{Plain list|* {{Rust|Box<T>}}
* {{Rust|Option<Box<T>>}}}}
|A pointer to heap-allocated value
(or possibly null pointer if wrapped in option)<ref name="std::boxed"/>
|{{Plain list|* {{Rust|let boxed: Box<u8> {{=}} Box::new(0);}}
* {{Rust|let boxed: Option<Box<String>> {{=}} Some(Box::new("Hello World"));}}}}
|-
|{{Plainlist|
* {{Rust|*const T}}
* {{Rust|*mut T}}
}}
|{{Plainlist|
* Raw pointers (immutable and mutable)
* Possibly [[Null pointer|null]]; {{Rust|unsafe}} to [[dereference]]
}}
|{{Plainlist|
* {{Rust|let x_ptr: *const T {{=}} &x as *const T;}}
* {{Rust|let x_ptr: *mut T {{=}} &mut x as *mut T;}}
}}
|}
 
To prevent the use of [[null pointer]]s and their [[Null pointer#Null dereferencing|dereferencing]], the basic <code>&</code> and <code>&mut</code> references are guaranteed to not be null. Rust instead uses <code>Option</code> for this purpose: <code>Some(T)</code> indicates that a value is present, and <code>None</code> is analogous to the null pointer.{{sfn|Klabnik|Nichols|2019|pp=101–104}} <code>Option</code> implements a "null pointer optimization", avoiding any spatial overhead for types that cannot have a null value (references or the <code>NonZero</code> types, for example).{{sfn|Gjengset|2021|p=145}}
 
TheRust <code>&</code>also and {{rust|&mut}} reference types are guaranteed to not be null and points to valid memory.{{sfn|Gjengset|2021|p=155-156}} Thesupports raw pointer types {{rust|<code>*const}}</code> and {{rust|<code>*mut}} opts out of the safety guarantees</code>, thus theywhich may be null and invalid; however, it is impossible to dereference them unless the code is explicitly declared unsafe through the use of an {{rust|<code>unsafe}}</code> block.{{sfn|Klabnik|Nichols|2023|pp=421-423}} Unlike dereferencing, the creation of raw pointers is allowed inside of safe Rust code.{{sfn|Klabnik|Nichols|2019|pp=418–427}}
 
=== Type conversion ===
Line 586 ⟶ 639:
[[File:Cargo compiling.webm|thumb|right|Compiling a Rust program with Cargo]]
 
The Rust ecosystem includes its compiler, its [[#Standard library|standard library]], and additional components for software development. Component installation is typically managed by {{code|rustup}}, a Rust [[toolchain]] installer developed by the Rust project.{{sfn|Blandy|Orendorff|Tindall|2021|pp=6-8}}
<!-- Add Miri Compiler? -->