fnmain() { leta = 233i32; println!("{}", f(&a)); }
这份代码是编译不过的:
error[E0515]: cannot return reference to local variable `aa` --> life_time.rs:13:5 | 13 | aa.a() | ^^^^^^ returns a reference to data owned by the current function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0515`.
fnf(a: &muti32) -> &muti32 { letmut aa = A { a }; aa.a() }
fnmain() { letmut a = 233i32; *f(&mut a) = 2333; println!("{}", a); }
这次我们指定了返回值的lifetime。但是仍然有编译报错:
error[E0515]: cannot return reference to local variable `aa` --> lifetime_mut.rs:13:5 | 13 | aa.a() | ^^^^^^ returns a reference to data owned by the current function
error: aborting due to previous error
For more information about this error, try `rustc --explain E0515`.
fnmain() { letmut a = 233i32; letmut aa = A { a: &mut a }; *f(&mut aa) = 2333; println!("{}", a); }
error[E0623]: lifetime mismatch --> move_ref_mut.rs:6:5 | 5 | fn f<'a, 'b>(aa: &'b mut A<'a>) -> &'a mut i32 { | ------------- ----------- | | | this parameter and the return type are declared with different lifetimes... 6 | aa.a | ^^^^ ...but data from `aa` is returned here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0623`.