Guest User

Untitled

a guest
Jan 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. // Q: why do we need to first assign a vector to a variable and use the variable
  2. // to do things to the vector? Why can't we do things directly to the vector?
  3. // See code below for examples.
  4. // It seems Rust was intentionally designed in this fashion because the first
  5. // rule of ownership is
  6. // "Each value in Rust has a variable that’s called its owner."
  7.  
  8. fn main() {
  9. let mut v = vec![1];
  10. v.push(2);
  11. println!("{:?}", v);
  12.  
  13. // why can't we just do one of the following?
  14. println!("{:?}", vec![1].push(2)); // () because ?
  15. println!("{:?}", (&mut vec![1]).push(2)); // () because ?
  16. }
Add Comment
Please, Sign In to add comment