On this page
  1. Clone
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

Clone

Prev: Copy | Up: builtins | Next: Drop
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Clone
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Trait) — available with zero use imports.

Syntax & Overview

fn clone(&self) -> Self

Explicitly duplicates a value (potentially performing a heap allocation).

Notes & Pitfalls

Example

#[derive(Clone, Debug, PartialEq)]
struct User {
    name: String,
}

fn main() {
    let original = User { name: String::from("Ada") };
    let copy = original.clone(); // original remains valid
    assert_eq!(original, copy);
}

Linked from

Related notes