Clone और Copy
clone() आपके data की एक असली दूसरी copy बनाता है, जबकि Copy types इतने छोटे हैं कि Rust अपने आप इन्हें duplicate कर देता है।In this page:
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
fn main() {
let a = 5;
let b = a; // a is copied, not moved
println!("a = {}, b = {}", a, b);
}
Login to try C/C++/Java/PHP code in the editor
Cloning Non-Copy Types
String और Vec जैसे types Copy implement नहीं करते क्योंकि उन्हें copy करना महंगा हो सकता है। .clone() call करना explicitly एक पूरी deep copy perform करता है, original और clone दोनों को valid और independent छोड़ते हुए।
उदाहरण: Cloning Non-Copy Types
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- आदतन हर value पर
.clone()call करना, यहां तक कि integers जैसे simpleCopytypes भी जो अपने आप duplicate हो जाते हैं। - यह भूल जाना कि किसी बड़े data structure पर
.clone()महंगा हो सकता है क्योंकि यह एक असली, पूरी deep copy perform करता है। - यह मान लेना कि एक custom struct अपने आप
Copyहै -- इसे explicitlyCopyऔरCloneदोनों derive करना चाहिए और सिर्फCopyfields रखनी चाहिए।
Copytypes (जैसे integers, floats, booleans, और chars) assignment पर move होने के बजाय अपने आप duplicate होते हैं।.clone()explicitly किसी value की एक पूरी, independent deep copy बनाता है, और किसी भी type पर call किया जा सकता है जोCloneimplement करता है।- एक struct सिर्फ तब
Copyderive कर सकता है जब इसकी हर 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: