Quantcast
Channel: Lifetime when exchanging reference - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Lifetime when exchanging reference

$
0
0

I'm learning Rust and play through some scenarios to see how it behaves. When passing a mutable reference to a function and then assign another mutable reference to it, it seems like the Rust compiler wants the referenced variable to live as long as the one originally referenced.

However, as I am literally exchanging the reference to point somewhere else, it would never affect the reference of the calling function. It will always still point to foo, which is perfectly alive. Why does foo2 have to live as long as foo then? This seems like it expects something like replacing the reference in the original reference ...

struct Foo {    x: u32,    y: u32}fn do_something(mut foo: &mut Foo) {    foo.x = 4;    foo.y = 5;    println!("foo.x = {}\nfoo.y = {}", foo.x, foo.y);    let mut foo2 = Foo{x: 10, y: 20};    foo = &mut foo2;    println!("foo.x = {}\nfoo.y = {}", foo.x, foo.y);}fn main() {    let mut foo = Foo{x: 1, y: 2};    do_something(&mut foo);    println!("foo.x = {}\nfoo.y = {}", foo.x, foo.y);}

This is what the compiler says:

error[E0597]: `foo2` does not live long enough  --> main.rs:11:11   |6  | fn do_something(mut foo: &mut Foo) {   |                          - let's call the lifetime of this reference `'1`...10 |     let mut foo2 = Foo{x: 10, y: 20};   |         -------- binding `foo2` declared here11 |     foo = &mut foo2;   |     ------^^^^^^^^^   |     |     |   |     |     borrowed value does not live long enough   |     assignment requires that `foo2` is borrowed for `'1`12 |     println!("foo.x = {}\nfoo.y = {}", foo.x, foo.y);13 | }   | - `foo2` dropped here while still borrowederror: aborting due to previous errorFor more information about this error, try `rustc --explain E0597`.

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images