April 2026
Intermediate
631 pages
16h 20m
English
The references we’ve created so far have been immutable. For instance, consider the code shown in Listing 4.29.
fn main() { let mut vec_1 = vec![1, 2, 3]; let mut vec_2 = vec![4, 5, 6]; let reference = &vec_1; reference = &vec_2; // Error}
Listing 4.29 Immutable Binding of a Reference
In this case, the variable reference itself is immutable, and we cannot update it to point to some other vector. Updating the reference to point to vec_2, therefore, throws an error. This error arises because we have an immutable binding of an immutable reference. To enable mutation of a reference itself, you can make the reference mutable, as shown in Listing 4.30.
fn main() { let mut vec_1 = vec![1, 2,
Read now
Unlock full access