← Back to Rust Course | Chapter 5: Ownership | Lesson 3 of 6

Clone और Copy

clone() आपके data की एक असली दूसरी copy बनाता है, जबकि Copy types इतने छोटे हैं कि Rust अपने आप इन्हें duplicate कर देता है।
Syntax
rust
let b = a.clone();  // deep copy

#[derive(Clone, Copy)]
struct Name {
    field: Type,
}

Copy Types

i32, f64, bool, और char जैसे simple, fixed-size types Copy trait implement करते हैं, मतलब assignment value को move करने के बजाय duplicate करती है -- दोनों variables independently usable रहती हैं।

उदाहरण: Copy Types

markup
fn main() {
    let a = 5;
    let b = a; // a is copied, not moved
    println!("a = {}, b = {}", a, b);
}

Cloning Non-Copy Types

String और Vec जैसे types Copy implement नहीं करते क्योंकि उन्हें copy करना महंगा हो सकता है। .clone() call करना explicitly एक पूरी deep copy perform करता है, original और clone दोनों को valid और independent छोड़ते हुए।

उदाहरण: Cloning Non-Copy Types

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `original`, set to `String::from("clone me")`
    let original = String::from("clone me");
    // Declare an immutable variable `cloned`, set to `original.clone()`
    let cloned = original.clone();
    // Print "original: {}, cloned: {}" to the console, substituting in `original, cloned` (with a trailing newline)
    println!("original: {}, cloned: {}", original, cloned);
}

Deriving Copy and Clone on a Struct

एक custom struct #[derive(Copy, Clone)] से Copy semantics opt in कर सकता है, लेकिन सिर्फ अगर इसमें मौजूद हर field खुद Copy हो। यह छोटे value-जैसे structs को built-in numeric types की तरह behave कराता है।

उदाहरण: Deriving Copy and Clone on a Struct

markup
// Auto-generate #[derive(Copy, Clone, Debug)] for the type below
#[derive(Copy, Clone, Debug)]
// Define a struct `Point` to group related fields together
struct Point {
    x: i32,
    y: i32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p1`, set to `Point { x: 1, y: 2 }`
    let p1 = Point { x: 1, y: 2 };
    // Declare an immutable variable `p2`, set to `p1`
    let p2 = p1;
    // Print "{:?} and {:?}" to the console, substituting in `p1, p2` (with a trailing newline)
    println!("{:?} and {:?}", p1, p2);
}

Clone Is Explicit, Copy Is Implicit

Copy eligible types के लिए assignment पर चुपचाप होता है, जबकि .clone() को हमेशा एक explicit method call चाहिए। यह visible distinction readers को code में potentially expensive deep-copy operations ढूंढने में मदद करता है।

उदाहरण: Clone Is Explicit, Copy Is Implicit

markup
// Auto-generate #[derive(Clone, Debug)] for the type below
#[derive(Clone, Debug)]
// Define a struct `Config` to group related fields together
struct Config {
    name: String,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `config1`, set to `Config { name: String::from("prod") }`
    let config1 = Config { name: String::from("prod") };
    // Declare an immutable variable `config2`, set to `config1.clone()`
    let config2 = config1.clone();
    // Print "{:?} vs {:?}" to the console, substituting in `config1, config2` (with a trailing newline)
    println!("{:?} vs {:?}", config1, config2);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. आदतन हर value पर .clone() call करना, यहां तक कि integers जैसे simple Copy types भी जो अपने आप duplicate हो जाते हैं।
  2. यह भूल जाना कि किसी बड़े data structure पर .clone() महंगा हो सकता है क्योंकि यह एक असली, पूरी deep copy perform करता है।
  3. यह मान लेना कि एक custom struct अपने आप Copy है -- इसे explicitly Copy और Clone दोनों derive करना चाहिए और सिर्फ Copy fields रखनी चाहिए।
चैप्टर सारांश
  • Copy types (जैसे integers, floats, booleans, और chars) assignment पर move होने के बजाय अपने आप duplicate होते हैं।
  • .clone() explicitly किसी value की एक पूरी, independent deep copy बनाता है, और किसी भी type पर call किया जा सकता है जो Clone implement करता है।
  • एक struct सिर्फ तब Copy derive कर सकता है जब इसकी हर field भी Copy हो।
  • Copy पर भरोसा करने और .clone() call करने के बीच चुनना एक performance और design decision है, सिर्फ syntax choice नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.