Skip to content

Instantly share code, notes, and snippets.

@drewcrawford
Last active August 29, 2015 14:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save drewcrawford/c88d478088d8de412dcf to your computer and use it in GitHub Desktop.
#[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