fnfoo(x: &i32, y: &i32) { lettmp = x; x = y; y = tmp; println!("{} {}", x, y); }
fnmain() { letx = 1; lety = 2; foo(&x, &y); }
就会报lifetime有关的错误:
error[E0623]: lifetime mismatch --> learn_swap_two_references.rs:4:9 | 1 | fn foo(mut x: &i32, mut y: &i32) { | ---- ---- | | | these two types are declared with different lifetimes... ... 4 | y = tmp; | ^^^ ...but data from `x` flows into `y` here
error[E0623]: lifetime mismatch --> learn_swap_two_references.rs:3:9 | 1 | fn foo(mut x: &i32, mut y: &i32) { | ---- ---- these two types are declared with different lifetimes... 2 | let tmp = x; 3 | x = y; | ^ ...but data from `y` flows into `x` here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0623`.