Skip to content
Theory · Ownership · lesson 5 of 8

clone and Copy

In the previous lesson, let t = s; handed over the key to a unit, and there was still only one string. Sometimes you need something else: two independent strings, so you can keep the original as it is and change the copy.

Picture renting a second unit at the facility and putting copies of all the things from the first one into it. Now you have two units, two keys and two sets of things. Whatever you do with the things in the second unit, nothing changes in the first. For a string, the clone method does this.

clone: a second unit

Rust
fn main() {
    let original = String::from("hi");
    let mut copy = original.clone();
    copy.push('!');
    println!("{original} {copy}"); // hi hi!
}

Let's go line by line:

  1. original becomes the owner of the string hi.
  2. original.clone() takes a new unit at the facility and copies the bytes h and i into it. The owner of the new string is copy.
  3. clone only reads original and does not take its key away, so original stays usable.
  4. copy.push('!') adds a character only to the unit of copy.
  5. The program prints hi hi!: the original has not changed.

Such a copy is called deep: not only the card on the stack is copied, but also the bytes on the heap it points to. The second rule of ownership still holds. There are now two values, and each has its own owner, its own unit and its own key. At }, each owner will give back its own unit.

Why clone is written explicitly

Copying a long string costs time and memory: it needs a new unit and a transfer of all the bytes. That is why Rust never copies a String on its own, silently. The expensive operation is always visible in the code as a call to .clone().

Beginners sometimes put .clone() wherever the compiler complains about a move. The program builds, but each such copy is a new unit on the heap. Write clone when you really need two independent strings.

Copy types

Small values that sit entirely on the stack are copied by Rust on its own, like a photocopy. A Copy type is a type whose values are copied bit for bit on assignment instead of being moved. Rust records the Copy property as a trait: a mark on a type that tells the compiler what the type can do.

The Copy types are:

  • integers: i32, u8, i64, usize and others;
  • floating-point numbers, f64;
  • bool and char;
  • tuples and arrays whose elements are all Copy.
Rust
let point = (3, 4.5);
let other = point;
println!("{:?} {:?}", point, other); // (3, 4.5) (3, 4.5)

The tuple (3, 4.5) consists of an i32 and an f64, so let other = point; copies it, and both variables are printed.

A String cannot be Copy. A bit-for-bit copy would repeat only the card, and one unit would end up with two keys, which means a double return. That is why a type that owns data on the heap is always moved on assignment.

Here the photocopy analogy falls short. A photocopy is usually worse than the original, while a copy of a Copy value is no different from it at all: it is the very same bytes.

Interviewers like to ask this: "How does Copy differ from Clone?" The answer:

  • Copy is an implicit bit-for-bit copy on assignment, only for values without data on the heap;
  • Clone is a trait with the .clone() method: the copy is made explicitly, and it may take new memory and be expensive;
  • every Copy type also has clone, but not every type with clone can be Copy.

Common mistake

A tuple is expected to be copied like numbers, but it holds a String:

Rust
fn main() {
    let pair = (1, String::from("bike"));
    let other = pair;
    println!("{:?} {:?}", pair, other); // ✗
}
TEXT
error[E0382]: borrow of moved value: `pair`

A tuple is Copy only if all its elements are Copy. Here one element is a String, so let other = pair; moves the whole tuple. If you need both, make the copy explicitly: with let other = pair.clone(); the program prints (1, "bike") (1, "bike").

Key points

  • clone creates a second unit on the heap with a copy of the bytes, and the copy does not depend on the original.
  • clone only reads the original, and the original stays usable.
  • clone is written explicitly because copying data from the heap costs time and memory.
  • Copy types (numbers, bool, char and tuples of them) are copied on assignment, while a type with data on the heap, like String, is moved.