ProgramGuru

Copy values and deliberate cloning

Copy values and deliberate cloning

A mark is a small integer; a student name may own allocated text. Assigning the integer leaves both bindings usable because its type implements Copy. String does not implement Copy. Clone explicitly asks a type to produce another value and can involve allocation or other work.

What you’ll learn

  • Predict whether an assignment copies or moves a value.
  • Explain why String cloning and Rc cloning have different effects.

Copy values and deliberate cloning in action

  1. Integer: copy 80

    Both integer bindings contain 80.

    Integer: copy 80
  2. String: clone Diya

    Cloning creates another String with the same initial text.

    String: clone Diya
  3. Change label only

    push_str appends to label, leaving name unchanged.

    Change label only
Both integer bindings contain 80. Cloning creates another String with the same initial text. push_str appends to label, leaving name unchanged. The example performs an intentional clone rather than using cloning to silence an unexplained ownership error.

Copy values and deliberate cloning explained

For an i32, assignment copies the integer value. For String, clone() creates independent owned text, so changing one does not change the other. Clone behavior depends on the type: cloning an Rc increases shared ownership rather than duplicating its underlying record. Read the type contract before assuming a deep copy.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

fn main() {
  let mark = 80;
  let copied = mark;
  let name = String::from("Diya");
  let mut label = name.clone();
  label.push_str(" - A");
  println!("{mark} {copied}");
  println!("{name}");
  println!("{label}");
}

Expected output

80 80
Diya
Diya - A

Follow the execution

Both integer bindings contain 80. Cloning creates another String with the same initial text. push_str appends to label, leaving name unchanged. The example performs an intentional clone rather than using cloning to silence an unexplained ownership error.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: Will appending to label change name in this example?

Rust 2024 · complete program

fn main() {
  let mark = 80;
  let copied = mark;
  let name = String::from("Diya");
  let mut label = name.clone();
  label.push_str(" - A");
  println!("{mark} {copied}");
  println!("{name}");
  println!("{label}");
}

Worked answer

No. String::clone creates independent owned text. Both initially contain Diya, but modifying label does not append to name.

Mistakes and limits to recognize

Copy means implicit value copying is supported, not that every type is cheap or every nested value is independently duplicated. Prefer borrowing for read-only access when duplication serves no purpose. A type with Drop cannot implement Copy.

Check your understanding

Will appending to label change name in this example?
Choose one answer. You can try again.

Answer and explanation

No.

No. String::clone creates independent owned text. Both initially contain Diya, but modifying label does not append to name.

Take the idea into your next program

No. String::clone creates independent owned text. Both initially contain Diya, but modifying label does not append to name. Copy means implicit value copying is supported, not that every type is cheap or every nested value is independently duplicated.

Further reading: the official Rust Book on copy values and deliberate cloning.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: References and read-only borrowing →