We cannot modify a collection once we have a reference on it:
let mut v = vec![1, 2, 3];
let elem = &v[0]; // immutable borrow
v.push(10); // mutable borrow ==> COMPILER ERROR!
(elem, v)Error: The variable `elem` contains a reference with a non-static lifetime so
can't be persisted. You can prevent this error by making sure that the
variable goes out of scope - i.e. wrapping the code in {}.[E0502] Error: cannot borrow `v` as mutable because it is also borrowed as immutable
╭─[ command_2:1:1 ]
│
3 │ v.push(10); // mutable borrow ==> COMPILER ERROR!
│ ─────┬────
│ ╰────── mutable borrow occurs here
│
├─[ command_2:1:1 ]
│
2 │ let elem = &v[0]; // immutable borrow
│ ┬
│ ╰── immutable borrow occurs here
│
│ Note: note: requirement that the value outlives `'static` introduced here
───╯[E0505] Error: cannot move out of `v` because it is borrowed
╭─[ command_2:1:1 ]
│
4 │ (elem, v)
│ ┬
│ ╰── move out of `v` occurs here
│
├─[ command_2:1:1 ]
│
2 │ let elem = &v[0]; // immutable borrow
│ ┬
│ ╰── borrow of `v` occurs here
│
├─[ command_2:1:1 ]
│
1 │ let mut v = vec![1, 2, 3];
│ ──┬──
│ ╰──── binding `v` declared here
2 │ let elem = &v[0]; // immutable borrow
│ │
│ ╰─ help: consider cloning the value if the performance cost is acceptable: `.clone()`
───╯