shanu
May 16, 2024, 5:20am
1
Hey All, hope you all are great.
I just started learning Rust and was going through borrowing and ownerships and i stumbled upon this code
let mut v1: Vec<u32> = vec![1,2,3,4];
let mut v2: Vec<u32> = vec![10,11,12];
println!("vector before append :{:?}, {:?}",v,v2);
v.append(&mut v2);
println!("vector after append :{:?}, {:?}",v,v2);
v2.push(25);
let mut v3: Vec<u32> = Vec::new();
v3.append(&mut v2);
since a variable cannot share mut reference twice, how does this compile without an error ?
There cannot be two simultaneously active mutable references. But in this case, they are not active at the same time, since append
releases the reference passed to it after returning, it doesn't store this reference anywhere.
1 Like
shanu
May 16, 2024, 5:49am
3
Thanks !
Is there a reference i can refer ?
I have some notes here. The full details of in-function borrow checking are quite involved but I can give a few citations if you want.
1 Like
system
Closed
August 14, 2024, 5:56am
5
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.