You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lifetimes of a function are treated differently between Initialization
let myFunctor:&Fn(i32) -> i32 = &fx ;
and Assignment
let myFunctor:&Fn(i32) -> i32;
myFunctor= &fx ;
See the below example
fnfx(x:i32) -> i32{
x
}fntest_Functor(){// case 1. Initializationlet myFunctor:&Fn(i32) -> i32 = &fx ;// case 2. Assignment//let myFunctor: &Fn(i32) -> i32;//myFunctor= &fx ;}
This can compile.
If I comment case 1 and uncomment case 2. I got an error
Compiling study_rust v0.0.1 (main.rs)
src/main.rs:45:17: 45:19 error: borrowed value does not live long enough
src/main.rs:45 myFunctor= &fx ;
^~
src/main.rs:44:36: 47:2 note: reference must be valid for the block suffix following statement 0 at 44:35...
src/main.rs:44 let myFunctor: &Fn(i32) -> i32;
src/main.rs:45 myFunctor= &fx ;
src/main.rs:46
src/main.rs:47 }
src/main.rs:45:5: 45:21 note: ...but borrowed value is only valid for the statement at 45:4
src/main.rs:45 myFunctor= &fx ;
^~~~~~~~~~~~~~~~
src/main.rs:45:5: 45:21 help: consider using a `let` binding to increase its lifetime
src/main.rs:45 myFunctor= &fx ;
^~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `study_rust`.
Is it a bug? I am using rustc 1.5.0
rustc 1.5.0 (3d7cd77e4 2015-12-04)
The text was updated successfully, but these errors were encountered:
Indeed, as @bluss suggests, this is fallout from our rules about the scope used for temporary values. In the case of a let initializer, the scope is the enclosing block, but in the assignment that is not the case. I would classify this as "not a bug" -- or, at least, obeying the rules as designed, for better or worse. One could imagine tweaking the temporary lifetime rules to better handle cases like these, but it'd be an RFC-worthy change I would think.
Lifetimes of a function are treated differently between
Initialization
and Assignment
See the below example
This can compile.
If I comment case 1 and uncomment case 2. I got an error
Is it a bug? I am using rustc 1.5.0
The text was updated successfully, but these errors were encountered: