Guest User

Untitled

a guest
Aug 19th, 2019
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2. let s1 = gives_ownership(); // gives_ownership moves its return
  3. // value into s1
  4.  
  5. let s2 = String::from("hello"); // s2 comes into scope
  6.  
  7. let s3 = takes_and_gives_back(s2); // s2 is moved into
  8. // takes_and_gives_back, which also
  9. // moves its return value into s3
  10. } // Here, s3 goes out of scope and is dropped. s2 goes out of scope but was
  11. // moved, so nothing happens. s1 goes out of scope and is dropped.
  12.  
  13. fn gives_ownership() -> String { // gives_ownership will move its
  14. // return value into the function
  15. // that calls it
  16.  
  17. let some_string = String::from("hello"); // some_string comes into scope
  18.  
  19. some_string // some_string is returned and
  20. // moves out to the calling
  21. // function
  22. }
  23.  
  24. // takes_and_gives_back will take a String and return one
  25. fn takes_and_gives_back(a_string: String) -> String { // a_string comes into
  26. // scope
  27.  
  28. a_string // a_string is returned and moves out to the calling function
  29. }
RAW Paste Data