Guest User

Untitled

a guest
Apr 27th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. fn main() {
  2. let s = String::from("hello"); // s comes into scope.
  3.  
  4. takes_ownership(s); // s's value moves into the function...
  5. // ... and so is no longer valid here.
  6.  
  7. println!("{}", s);
  8.  
  9. let x = 5; // x comes into scope.
  10.  
  11. makes_copy(x); // x would move into the function,
  12. // but i32 is Copy, so it’s okay to still
  13. // use x afterward.
  14.  
  15. } // Here, x goes out of scope, then s. But since s's value was moved, nothing
  16. // special happens.
  17.  
  18. fn takes_ownership(some_string: String) { // some_string comes into scope.
  19. println!("{}", some_string);
  20. } // Here, some_string goes out of scope and `drop` is called. The backing
  21. // memory is freed.
  22.  
  23. fn makes_copy(some_integer: i32) { // some_integer comes into scope.
  24. println!("{}", some_integer);
  25. } // Here, some_integer goes out of scope. Nothing special happens.
Add Comment
Please, Sign In to add comment