Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 17, 2021 18:16
Show Gist options
  • Save rust-play/a92d5af0c2f8fd54b26950dbc3ce3ac1 to your computer and use it in GitHub Desktop.
Save rust-play/a92d5af0c2f8fd54b26950dbc3ce3ac1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// use std::cell::RefCell;
// Ownership
use {
std::ops::Drop
};
#[derive(Debug)]
struct Parent(usize, Child, Child);
#[derive(Debug)]
struct Child(usize);
impl Drop for Parent {
fn drop(&mut self) {
println!("Dropping {:?}", self);
}
}
impl Drop for Child {
fn drop(&mut self) {
println!("Dropping {:?}", self);
}
}
fn main() {
//let mut rc1 = Rc::new(Child(1));
let p1 = Parent(1, Child(11), Child(12));
let p2 = p1;
println!("a: p2:{:?}", p2);
println!("a: p1:{:?}", p1);
// |
//29 | let p1 = Parent(1, Child(11), Child(12));
// | -- move occurs because `p1` has type `Parent`, which does not implement the `Copy` trait
//30 | let p2 = p1;
// | -- value moved here
//31 | println!("a: p2:{:?}", p2);
//32 | println!("a: p1:{:?}", p1);
// | ^^ value borrowed here after move
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment