Skip to content

Instantly share code, notes, and snippets.

Created April 14, 2016 14:20
Show Gist options
  • Save anonymous/b73e42f7b2f6a33a694b7d9269a1a72c to your computer and use it in GitHub Desktop.
Save anonymous/b73e42f7b2f6a33a694b7d9269a1a72c to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::mem::{replace};
#[derive(Clone, Debug)]
struct HasDrop(i32);
impl Drop for HasDrop {
fn drop(&mut self) {
println!("Dropping {}!", self.0);
}
}
fn bye<T>(_: T) {}
fn kill_and_get<T>(mut v : Vec<T>, i: usize) -> T {
v.swap_remove(i)
}
fn main() {
let mut z = Box::new([1; 20]);
let z2 = z.clone();
z[10] = 5;
println!("{}",z2[10] + z[10]);
let x = vec![HasDrop(1), HasDrop(2)];
let str = {format!("{:?}",x.get(1).cloned())};
println!("{}",str);
println!("a");
let y = kill_and_get(x, 1);
println!("c");
bye(y);
println!("d");
let mut _a = vec![HasDrop(3),HasDrop(4)];
replace(&mut _a[1],HasDrop(5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment