Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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. let x = 5; // x comes into scope
  8.  
  9. makes_copy(x); // x would move into the function,
  10. // but i32 is Copy, so it’s okay to still
  11. // use x afterward
  12. } // Here, x goes out of scope, then s. But because s's value was moved, nothing
  13. // special happens.
  14.  
  15. fn takes_ownership(some_string: String) {
  16. // some_string comes into scope
  17. println!("{}", some_string);
  18. } // Here, some_string goes out of scope and `drop` is called. The backing
  19. // memory is freed.
  20.  
  21. fn makes_copy(some_integer: i32) {
  22. // some_integer comes into scope
  23. println!("{}", some_integer);
  24. } // Here, some_integer goes out of scope. Nothing special happens.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement