BoxesEdit on GitHub
Boxes can be used for adding a mutable property to an immutable data structure, like a tuple or a list.
let mut should be preferred when creating mutable value bindings.
Name to be used for the box binding.
Any valid Grain expression.
boxallows you to create a reference to a value.
unboxallows you to access the value inside a box.
:=allows you to assign a reference to a new value inside the box.
Take a look at the following example:
A couple things to note from this example:
- We use the built-in
boxfunction to create a box.
- We use
unboxto retrieve a value from a box. This is commonly called “unboxing.”
- We use
:=to update the value in a box.
let bindings in Grain, boxes are immutable. The box itself cannot be swapped out for another box (or anything else for that matter). However, the contents of boxes can be changed.