Content deleted Content added
→Pointers: using syntax highlighted snippets where possible; c/e |
enums in rust are actually tagged unions Tags: Reverted 2017 wikitext editor |
||
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, [[
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
<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}}
▲
=== Type conversion ===
Line 586 ⟶ 639:
[[File:Cargo compiling.webm|thumb|right|Compiling a Rust program with Cargo]]
The Rust ecosystem includes its compiler, its [[
<!-- Add Miri Compiler? -->
|