Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copy Types vs. Move Types
- // Instances of data that are easily copyable (and relatively cost free)
- // Such as all of the primitive numeric types, or a char.
- // are simply copied into places where types would otherwise be moved.
- // The rules for borrowing a copy type remain exactly the same.
- fn copy_and_display(x: i32) {
- println!("x = {}", x);
- }
- fn move_and_display(s: String) {
- println!("s = {}", s);
- }
- fn main() {
- let x = 5;
- copy_and_display(x);
- // We can still print x, because it was copied into the function, not moved.
- println!("x = {}", x);
- let s = String::from("this is a move type");
- move_and_display(s);
- // We can no longer display s because it was moved into the function.
- // println!("s = {}", s);
- }
Add Comment
Please, Sign In to add comment