Skip to content

Instantly share code, notes, and snippets.

@bugaevc bugaevc/
Created Apr 27, 2016

What would you like to do?
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 {
// at this point, v is using
// no less than 4000 bytes of memory
// -------------------
// transfer ownership to print_sum:
// 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
You can’t perform that action at this time.