Skip to main content
Be Bitwise
RustRust Basics & Ownership

Move & Clone

Need a value in two places? Rust offers two doors: hand the ownership across, or explicitly duplicate the data.

Clone: Deep Copy

rust
let s1 = String::from("hello");
let s2 = s1.clone(); // deep copy of the heap data
println!("{}", s1); // OK: s1 still owns its copy
println!("{}", s2); // OK: s2 owns a separate copy

clone() allocates fresh heap and copies the bytes. The cost is visible in the source — no implicit deep copies sneaking in.

Copy: Stack-Only

Types that opt into the Copy trait duplicate themselves on assignment, no fuss:

rust
let x: i32 = 42;
let y = x; // copy, not move
println!("{} {}", x, y); // both valid

The Copy club: every integer type, bool, f32/f64, char, plus tuples and fixed-size arrays whose elements are themselves Copy.

Key Concept

Copy versus Move is a semantics call, not just a performance one. Copy says: "this value is shallow enough that duplicating it is always cheap and always correct." Move says: "this value owns something — heap, file handle, lock — and silent duplication would be a bug."

Functions and Clone

If a function needs the value but you'd like to keep it around afterward, hand it a clone:

rust
fn print_string(s: String) {
println!("{}", s);
}
let s = String::from("hello");
print_string(s.clone()); // pass a clone
println!("{}", s); // s is still valid

Idiomatic Rust avoids the allocation altogether — you borrow instead. We get to that in the next module:

rust
fn print_string(s: &String) {
println!("{}", s);
}
let s = String::from("hello");
print_string(&s); // borrow, no clone needed
println!("{}", s); // s is still valid

Moving Out of Collections

rust
let v = vec![String::from("a"), String::from("b"), String::from("c")];
// This moves a single element out:
let first = v.into_iter().next().unwrap();
// v is no longer valid - into_iter() consumed it
// To keep the vector, use indexing with clone:
let v2 = vec![String::from("a"), String::from("b")];
let first = v2[0].clone(); // clone the element
println!("{}", first);
println!("{:?}", v2); // v2 is still valid

What you can't write is let first = v[0]; for a String element. Indexing only borrows, so the compiler stops you with E0507 (cannot move out of index). The runner shows three ways round it.

Try itUncomment each marked line in turn and read the error, then pick the fix that suits a vector you still need afterwards.
Rust (1.40.0)

Pattern: Return Ownership

A common workaround before borrowing clicks: return ownership back out:

rust
fn process(s: String) -> String {
println!("Processing: {}", s);
s // return ownership back to caller
}
let s = String::from("data");
let s = process(s); // s moves in, then moves back out
println!("{}", s); // s is valid again

It works, sure, but the ergonomics are awful. Borrowing — coming up next module — does this without the move-out-move-back dance.

Challenges

Demonstrate ownership by creating a String, moving it, and cloning it.

Rust (1.40.0)