Default, Clone, and Copy

Default, Clone, and Copy: filling a placeholder, explicit clones that are not always deep, and why Copy is not an ownership escape.

テーマページの本文は現在英語です。

Default builds a placeholder value, which is what mem::take leaves behind. Clone is an explicit copy you request with .clone(). It is often a deep copy of owned buffers, such as String or Vec, but it is not always: Rc::clone increments a reference count and shares the allocation. Copy is a marker trait that lets assignment duplicate a value implicitly with a bitwise copy.

Copy is not an ownership escape. After let copied = point, both names own the same kind of independent Point value; the original was not moved, and it was not borrowed into a longer lifetime. Types that implement Drop cannot be Copy. String is Clone but not Copy, so let taken = owned moves the String. Examples were compiled with rustc 1.90.0 and --edition=2024.

Examples were compiled with rustc 1.90.0 using --edition=2024 and run as a plain binary. See the Rust 1.90.0 documentation for the complete API.

Rust reference overview →

API index

APISummary
Default::defaultConstruct a default value for a type. Common numeric and collection types implement Default in the standard library.
Clone::clone and CopyRequest an explicit clone, or opt a bitwise-copy type into implicit duplication. Copy does not move ownership into a new lifetime.

Default fills a placeholder

Default::default() produces an agreed empty or zero value: 0 for integers, false for bool, "" for String, and an empty container for Vec. You can derive Default when every field implements Default. mem::take depends on this trait; that usage is on the mem page.

Default::default

pub trait Default {
    fn default() -> Self;
}

Construct a default value for a type. Common numeric and collection types implement Default in the standard library.

Returns: A new Self. For i32 this is 0; for Vec<T> it is an empty vector.

  • Some types have no meaningful default and simply do not implement the trait. take cannot be used on those types.
rust
fn main() {
    let number: i32 = Default::default();
    let empty: Vec<i32> = Default::default();
    println!("default={number} len={}", empty.len());
}

Expected output

default=0 len=0

Related: mem::take uses Default, Official Default

Official documentation →

Clone is explicit; Copy is not an escape

clone() is always visible in the source. Cloning a String or a Vec copies the buffer. Cloning an Rc shares the inner value: the example prints a strong count of 2 after the second handle is created, and no second Vec is allocated. That is a counterexample to "Clone is always a deep copy."

A type can implement Copy only if it also implements Clone and its fields are Copy. Assignment and passing by value then copy instead of move, so the original name stays usable. That still copies the bits; it does not extend a borrow or smuggle a value past its owner. A later use of a moved String is a compile error, because String is not Copy.

Clone::clone and Copy

pub trait Clone: Sized {
    fn clone(&self) -> Self;
}
pub trait Copy: Clone {}

Request an explicit clone, or opt a bitwise-copy type into implicit duplication. Copy does not move ownership into a new lifetime.

ParameterDescription
selfThe value to clone. Copy types are also duplicated by assignment without calling clone explicitly.

Returns: clone returns a new Self. For Rc, the new handle points at the same allocation. For String, the new value has its own buffer.

  • derive(Copy, Clone) is valid for Point because both fields are i32. After let copied = point, printing both names is allowed.
  • After let taken = owned, only taken can be printed. That move is the usual ownership rule, not a gap that Copy would "escape."
  • Interior mutability types such as Rc<RefCell<T>> are covered on the Rc and RefCell page. Cloning the Rc still shares the cell.
rust
use std::rc::Rc;

#[allow(dead_code)]
#[derive(Clone, Debug)]
struct Label(String);

#[allow(dead_code)]
#[derive(Copy, Clone, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let first = Label(String::from("a"));
    let second = first.clone();
    println!("clone={first:?} {second:?}");

    let point = Point { x: 1, y: 2 };
    let copied = point;
    println!("copy={point:?} {copied:?}");

    let owned = String::from("moved");
    let taken = owned;
    println!("moved={taken}");

    let shared = Rc::new(vec![1, 2]);
    let also = shared.clone();
    println!("rc_count={} {}", Rc::strong_count(&shared), also[0]);
}

Expected output

clone=Label("a") Label("a")
copy=Point { x: 1, y: 2 } Point { x: 1, y: 2 }
moved=moved
rc_count=2 1

Related: Rc and RefCell, Official Clone, Official Copy

Official documentation →