Guest User

Untitled

a guest
Feb 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // Copy Types vs. Move Types
  2.  
  3. // Instances of data that are easily copyable (and relatively cost free)
  4. // Such as all of the primitive numeric types, or a char.
  5. // are simply copied into places where types would otherwise be moved.
  6.  
  7. // The rules for borrowing a copy type remain exactly the same.
  8.  
  9. fn copy_and_display(x: i32) {
  10. println!("x = {}", x);
  11. }
  12.  
  13. fn move_and_display(s: String) {
  14. println!("s = {}", s);
  15. }
  16.  
  17. fn main() {
  18. let x = 5;
  19. copy_and_display(x);
  20.  
  21. // We can still print x, because it was copied into the function, not moved.
  22. println!("x = {}", x);
  23.  
  24. let s = String::from("this is a move type");
  25.  
  26. move_and_display(s);
  27.  
  28. // We can no longer display s because it was moved into the function.
  29. // println!("s = {}", s);
  30. }
Add Comment
Please, Sign In to add comment