[Doubt]New to Rust

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

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

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.