daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Feb 16th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copy 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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top