Last active
August 29, 2015 13:57
-
-
Save bvssvni/9376726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800) | |
/// A struct with borrowed pointer to a closure. | |
/// This is to be able to reuse the closure then struct runs out of scope. | |
pub struct Test<'a> { | |
f: &'a 'a || | |
} | |
impl<'a> Test<'a> { | |
pub fn new(f: &'a ||) -> Test<'a> { | |
Test { f: f } | |
} | |
} | |
pub struct Test2<'a> { | |
f: Option<&'a 'a ||> | |
} | |
impl<'a> Test2<'a> { | |
pub fn new(f: &'a ||) -> Test2<'a> { | |
Test2 { f: Some(f) } | |
} | |
} | |
fn main() { | |
let x = || println!("hello!"); | |
// Works | |
(*Test::new(&x).f)(); | |
// Fails | |
(*Test2::new(&x).f.unwrap())(); | |
} | |
Compiler error: | |
optionclosure.rs:21:3: 21:23 error: cannot infer an appropriate lifetime for region in type/impl due to conflicting requirements | |
optionclosure.rs:21 Test2 { f: Some(f) } | |
^~~~~~~~~~~~~~~~~~~~ | |
optionclosure.rs:21:14: 21:21 note: first, the lifetime cannot outlive the call at 21:13... | |
optionclosure.rs:21 Test2 { f: Some(f) } | |
^~~~~~~ | |
optionclosure.rs:21:14: 21:21 note: ...so that return value is valid for the call | |
optionclosure.rs:21 Test2 { f: Some(f) } | |
^~~~~~~ | |
optionclosure.rs:20:37: 22:3 note: but, the lifetime must be valid for the lifetime &'a as defined on the block at 20:36... | |
optionclosure.rs:20 pub fn new(f: &'a ||) -> Test2<'a> { | |
optionclosure.rs:21 Test2 { f: Some(f) } | |
optionclosure.rs:22 } | |
optionclosure.rs:21:3: 21:23 note: ...so that types are compatible (expected `Test2<'a>` but found `Test2<>`) | |
optionclosure.rs:21 Test2 { f: Some(f) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment