hd-lang

Tour · 13 of 25

A copy of a list never changes when the original does, and copying is cheap

A List, a Map and a string are values, like a struct. saved := cart is an undo point: pushing onto cart afterwards leaves saved alone, and a function that is given a cart can build its own changed cart without touching the caller's. Copies share their storage until one of them changes, and the change copies it first, so keeping a history of a thousand carts costs one copy per edit, not per snapshot. To change the caller's list on purpose, pass &mut cart.items, as a struct field is a place like any other, and write the &mut at the call.

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