I am thinking the idea behind the rules of ownership in Rust,
and what makes me confused is that:
Given
- reference is borrowing instead of owning;
- there is only one owner of
data;
The following code which use a mutable reference to change the data owner is still workable. Doesn’t this violate the previous rules ?
fn main() {
let mut v = vec![1, 2, 3];
//let ref_v = &v;
let ref_i = &mut v[0];
*ref_i = 10;
println!("{v:?}");
let mut v2 = vec![1, 2, 3];
//let ref_v = &v;
let ref_v2 = &mut v2;
*ref_v2 = vec![10, 20, 30];
println!("{v2:?}");
}