Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The clone() Function
- // Clone types allow you to explicitly invoke a .clone() method.
- // This copies the contents to a new instance, but it can be expensive.
- // If it wasn't expensive, the type itself would be a copy type.
- // Use borrowing instead of clone if you can avoid it.
- fn copy_and_display(x: i32) {
- println!("x = {}", x);
- }
- fn move_and_display(s: String) {
- println!("s = {}", s);
- }
- fn main() {
- let x = 5;
- // x is a Copy Type, but all copy types are clone types.
- copy_and_display(x.clone());
- // We can still print x, because it was copied into the function, not moved.
- println!("x = {}", x);
- let s = String::from("this is a clone type");
- // Here we move a clone of s into the function.
- move_and_display(s.clone());
- // We can no longer display s because it was moved into the function.
- println!("s = {}", s);
- }
Add Comment
Please, Sign In to add comment