Skip to content

Instantly share code, notes, and snippets.

@bugaevc
Created April 27, 2016 18:43
Show Gist options
  • Save bugaevc/fa32e766fd08265b4f07bef684494e11 to your computer and use it in GitHub Desktop.
Save bugaevc/fa32e766fd08265b4f07bef684494e11 to your computer and use it in GitHub Desktop.
fn print_sum(v: Vec<i32>) {
println!("{}", v[0] + v[1]);
// v is dropped and deallocated here
}
fn main() {
let mut v = Vec::new(); // creating the resource
for i in 1..1000 {
v.push(i);
}
// at this point, v is using
// no less than 4000 bytes of memory
// -------------------
// transfer ownership to print_sum:
print_sum(v);
// we no longer own nor anyhow control v
// it would be a compile-time error to try to access v here
println!("We're done");
// no deallocation happening here,
// because print_sum is responsible for everything
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment