| 1 | struct Tree { |
| 2 | mut: |
| 3 | garbage int |
| 4 | parent ?&Tree |
| 5 | } |
| 6 | |
| 7 | fn main() { |
| 8 | // `parent` is not declared as mutable! |
| 9 | parent := Tree{ |
| 10 | garbage: 11 |
| 11 | } |
| 12 | // taking a reference of `parent` and putting it under a `mut:` struct field |
| 13 | mut child := Tree{ |
| 14 | parent: &parent |
| 15 | } |
| 16 | |
| 17 | // unwrap the reference of `parent`, but declare it as `mut` |
| 18 | mut inner_parent := child.parent? |
| 19 | inner_parent.garbage = 777 // !we can mutate `parent` freely! |
| 20 | println(parent.garbage) // 777 |
| 21 | } |
| 22 |