Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. fn main() {
  2. let mut t: u64 = 0;
  3.  
  4. // Vec is reference to memory in the heap, v is on stack
  5. let mut v: Vec<u64> = Vec::with_capacity(100);
  6. assert_eq!(0, v.len());
  7. for i in 0..100 {
  8. v.push(i)
  9. }
  10. for e in v {
  11. t += e
  12. }
  13. // can't say 'drop(v);' here, because the vector gets "used up" in the
  14. // for loop (takes each element and moves it out into e). Cant' reuse v
  15. // without using 'v.clone()' or 'v.iter()'
  16.  
  17.  
  18. println!("Sum was {}", t);
  19. // implicit free call here; or can say 'drop(t);'
  20.  
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement