Created
April 27, 2016 18:43
-
-
Save bugaevc/fa32e766fd08265b4f07bef684494e11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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