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
How is it possible to explicitly state the type of the variable x inside the closure:
#[allow(unused_variables)]fnmain(){let slice1:&[i32] = &[1,2,3,4,5][..];// This works.let slice2 = (|x| x)(slice1);// This does not.//let slice3 = (|x: &[i32]| x)(slice1);}
My expectation is that the commented line should work, as it merely adds a type annotation. However, the compiler displays the following:
rustc 1.13.0 (2c6933acc 2016-11-07)
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> <anon>:9:31
|
9 | let slice3 = (|x: &[i32]| x)(slice1);
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 9:30...
--> <anon>:9:31
|
9 | let slice3 = (|x: &[i32]| x)(slice1);
| ^
note: ...so that expression is assignable (expected &[i32], found &[i32])
--> <anon>:9:31
|
9 | let slice3 = (|x: &[i32]| x)(slice1);
| ^
note: but, the lifetime must be valid for the call at 9:17...
--> <anon>:9:18
|
9 | let slice3 = (|x: &[i32]| x)(slice1);
| ^^^^^^^^^^^^^^^^^^^^^^^
note: ...so type `extern "rust-call" fn(&[closure@<anon>:9:18: 9:33], (&[i32],)) -> &[i32] {<[closure@<anon>:9:18: 9:33] as std::ops::Fn<(&[i32],)>>::call}` of expression is valid during the expression
--> <anon>:9:18
|
9 | let slice3 = (|x: &[i32]| x)(slice1);
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
Well, it seems that rustc needs an explicit lifetime spec for the reference. I don't know how to introduce a lifetime variable in the function body, but this does work:
You cannot do this today, you have to do what @ilammy mentioned.
rust-lang/rfcs#1650 would give you the ability to do this, or at least, pave the way for the syntax to exist. (the thread itself seems to be slightly conflicted about it). Closing in favor of that issue.
How is it possible to explicitly state the type of the variable
x
inside the closure:My expectation is that the commented line should work, as it merely adds a type annotation. However, the compiler displays the following:
It this not possible?
Link to Rust Playground: https://is.gd/KYIYfk
The text was updated successfully, but these errors were encountered: