hd-lang

Tour · 11 of 25

Two holders see one object only when you write &mut

Sometimes several parts of a program must change one thing: two checkout lanes selling from the same stock, a connection table, a screen's state. A plain field holds a copy, so each lane would sell the same five units. A field of type &mut Stock holds a reference to one object instead, and &mut Stock { ... } makes that object. Every holder of the reference sees the others' writes, and &mut is not exclusive, so there is no borrow to juggle. A reference keeps its object alive for as long as anyone holds it, so it can be stored and returned.

Run: Ctrl+Enter · Pages: Alt+← Alt+→