/gist:c88d478088d8de412dcf Secret
Last active
August 29, 2015 14:16
Star
You must be signed in to star a gist
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
#[derive(Clone)] | |
struct MyData { | |
x: i32 | |
} | |
trait MyDataTrait { | |
fn getX(&self) -> i32; | |
} | |
impl MyDataTrait for MyData { | |
fn getX(&self) -> i32 { | |
self.x | |
} | |
} | |
impl MyData { | |
fn foo<K: MyDataTrait + Clone + Send>(&self, otherFoo: &K) { //should return immediately | |
let mut clone = self.clone(); | |
let mut clone2 = otherFoo.clone(); | |
std::thread::Thread::spawn(move || { | |
println!("{:?}, {:?}",clone.x, clone2.getX()); //consider a very long operation | |
}); | |
} | |
} | |
fn main() { | |
let myData = MyData { x: 2 }; | |
myData.foo::<MyData> (&myData); | |
} | |
/*test.rs:20:37: 22:6 error: the parameter type `K` may not live long enough [E0310] | |
test.rs:20 std::thread::Thread::spawn(move || { | |
test.rs:21 println!("{:?}, {:?}",clone.x, clone2.getX()); //consider a very long operation | |
test.rs:22 }); | |
test.rs:20:37: 22:6 help: consider adding an explicit lifetime bound `K: 'static`... | |
test.rs:20 std::thread::Thread::spawn(move || { | |
test.rs:21 println!("{:?}, {:?}",clone.x, clone2.getX()); //consider a very long operation | |
test.rs:22 }); | |
test.rs:20:37: 22:6 note: ...so that captured variable `clone2` does not outlive the enclosing closure | |
test.rs:20 std::thread::Thread::spawn(move || { | |
test.rs:21 println!("{:?}, {:?}",clone.x, clone2.getX()); //consider a very long operation | |
test.rs:22 }); | |
error: aborting due to previous error | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment