Rust (programming language): Difference between revisions

Content deleted Content added
Ownership and references: rm example that isn't contributing much
Standard library: Add type annotations for consistency and clarity. For new readers these types are not necessarily clear.
Line 527:
|[[Reference counting]] pointer<ref>{{Cite web |title=Rc in std::rc |url=https://doc.rust-lang.org/beta/std/rc/struct.Rc.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080501/https://doc.rust-lang.org/beta/std/rc/struct.Rc.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let five: Rc<u8> = Rc::new(5);
let also_five: Rc<u8> = five.clone();
</syntaxhighlight>
|-
Line 534:
|[[Linearizability|Atomic]], [[Thread safety|thread-safe]] reference counting pointer<ref>{{Cite web |title=Arc in std::sync |url=https://doc.rust-lang.org/beta/std/sync/struct.Arc.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080503/https://doc.rust-lang.org/beta/std/sync/struct.Arc.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let foo: Arc<Vec<f32>> = Arc::new(vec![1.0, 2.0]);
let a: Arc<Vec<f32>> = foo.clone(); // a can be sent to another thread
</syntaxhighlight>
|-
Line 541:
|A mutable memory ___location<ref>{{Cite web |title=Cell in std::cell |url=https://doc.rust-lang.org/beta/std/cell/struct.Cell.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080506/https://doc.rust-lang.org/beta/std/cell/struct.Cell.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let c: Cell<u8> = Cell::new(5);
c.set(10);
</syntaxhighlight>
Line 548:
|A [[Lock (computer science)|mutex lock]] for shared data contained within.<ref>{{Cite web |title=Mutex in std::sync |url=https://doc.rust-lang.org/beta/std/sync/struct.Mutex.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080502/https://doc.rust-lang.org/beta/std/sync/struct.Mutex.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let mutex: Mutex<u32> = Mutex::new(0_u32);
let _guard: LockResult<MutexGuard<'_, u32>> = mutex.lock();
</syntaxhighlight>
|-
Line 555:
|[[Readers–writer lock]]<ref>{{Cite web |title=RwLock in std::sync |url=https://doc.rust-lang.org/beta/std/sync/struct.RwLock.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080503/https://doc.rust-lang.org/beta/std/sync/struct.RwLock.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let lock: RwLock<u8> = RwLock::new(5);
let r1: u8 = lock.read().unwrap();
</syntaxhighlight>
|-
Line 562:
|A [[Monitor (synchronization)|conditional monitor]] for shared data<ref>{{Cite web |title=Condvar in std::sync |url=https://doc.rust-lang.org/beta/std/sync/struct.Condvar.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080504/https://doc.rust-lang.org/beta/std/sync/struct.Condvar.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let (lock, cvar): (Mutex<bool>, Condvar) = (Mutex::new(true), Condvar::new());
// As long as the value inside the `Mutex<bool>` is `true`, we wait.
let _guard: Mutex<bool> = cvar.wait_while(lock.lock().unwrap(), |pending| { *pending }).unwrap();
 
</syntaxhighlight>
Line 577:
|[[Hash table]]<ref>{{Cite web |title=HashMap in std::collections |url=https://doc.rust-lang.org/beta/std/collections/struct.HashMap.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080505/https://doc.rust-lang.org/beta/std/collections/struct.HashMap.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let mut player_stats: HashMap<String, u32> = HashMap::new();
player_stats.insert("damage", 1);
player_stats.entry("health").or_insert(100);
Line 585:
|[[B-tree]]<ref>{{Cite web |title=BTreeMap in std::collections |url=https://doc.rust-lang.org/beta/std/collections/struct.BTreeMap.html |access-date=2023-06-24 |website=The Rust Standard Library documentation |archive-date=2023-06-24 |archive-url=https://web.archive.org/web/20230624080503/https://doc.rust-lang.org/beta/std/collections/struct.BTreeMap.html |url-status=live }}</ref>
|<syntaxhighlight lang="rust">
let mut solar_distance: BTreeMap<String, f32> = BTreeMap::from([
("Mercury", 0.4),
("Venus", 0.7),