Skip to content

Instantly share code, notes, and snippets.

@jviereck
Created February 17, 2016 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jviereck/aaef2ee55ceec58bce6f to your computer and use it in GitHub Desktop.
Save jviereck/aaef2ee55ceec58bce6f to your computer and use it in GitHub Desktop.
// Based on example from: https://doc.rust-lang.org/book/drop.html
struct HasDrop<'a> {
id: isize,
next: Option<&'a mut HasDrop<'a>>
}
// Implementing the |Drop| trait causes the errors shown below.
impl<'a> Drop for HasDrop<'a> {
fn drop(&mut self) {
println!("Dropping id={}!", self.id);
}
}
fn drop_mut_borrow<'a>(x: &'a mut HasDrop<'a>) {
let y = HasDrop { id: 1, next: Some(x) };
}
fn main() {
let mut x = HasDrop { id: 0, next: None };
drop_mut_borrow(&mut x);
}
// === Output from |rustc drop_example.rs|:
//
// drop_example.rs:22:26: 22:27 error: `x` does not live long enough
// drop_example.rs:22 drop_mut_borrow(&mut x);
// ^
// drop_example.rs:19:11: 23:2 note: reference must be valid for the block at 19:10...
// drop_example.rs:19 fn main() {
// drop_example.rs:20 let mut x = HasDrop { id: 0, next: None };
// drop_example.rs:21
// drop_example.rs:22 drop_mut_borrow(&mut x);
// drop_example.rs:23 }
// drop_example.rs:20:47: 23:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 20:46
// drop_example.rs:20 let mut x = HasDrop { id: 0, next: None };
//
// drop_example.rs:21
// drop_example.rs:22 drop_mut_borrow(&mut x);
// drop_example.rs:23 }
// error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment