Move Semantics क्या है
In this page:
let a = String::from("text");
let b = a; // a moved into b, a is no longer valid
Moving Ownership on Assignment
जब आप एक String (या अन्य heap-allocated, non-Copy type) को एक नए variable को assign करते हैं, Rust underlying data की ownership copy करने के बजाय move करता है, और original variable को invalidate कर देता है।
उदाहरण: Moving Ownership on Assignment
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `original`, set to `String::from("Rustacean")`
let original = String::from("Rustacean");
// Declare an immutable variable `moved`, set to `original`
let moved = original;
// Print "moved holds: {}" to the console, substituting in `moved` (with a trailing newline)
println!("moved holds: {}", moved);
}
Login to try C/C++/Java/PHP code in the editor
Moves Happen on Function Calls Too
किसी function में एक non-Copy value pass करना ownership को उस function के parameter में move करता है। Call के बाद, caller का variable तब तक valid नहीं है जब तक value वापस return न की जाए।
उदाहरण: Moves Happen on Function Calls Too
// Define the function `take_ownership` taking `s`
fn take_ownership(s: String) {
// Print "Function now owns: {}" to the console, substituting in `s` (with a trailing newline)
println!("Function now owns: {}", s);
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `text`, set to `String::from("hello")`
let text = String::from("hello");
take_ownership(text);
// Print "text was moved into the function" to the console (with a trailing newline)
println!("text was moved into the function");
}
Login to try C/C++/Java/PHP code in the editor
Getting a Value Back by Returning It
एक function किसी value की ownership वापस caller को return कर सकता है, call के बाद आपको इसे एक (संभवतः नए) variable नाम के तहत उपयोग करते रहने देते हुए।
उदाहरण: Getting a Value Back by Returning It
// Define the function `process` taking `s` and returning a String
fn process(s: String) -> String {
// Print "Processing: {}" to the console, substituting in `s` (with a trailing newline)
println!("Processing: {}", s);
s
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `text`, set to `String::from("data")`
let text = String::from("data");
// Declare an immutable variable `text`, set to `process(text)`
let text = process(text);
// Print "Back in main: {}" to the console, substituting in `text` (with a trailing newline)
println!("Back in main: {}", text);
}
Login to try C/C++/Java/PHP code in the editor
Why Rust Uses Moves Instead of Deep Copies
हर बार assign होने पर बड़ा heap-allocated data copy करना धीमा होगा। Moves Rust को ownership सस्ते में transfer करने देते हैं (सिर्फ एक pointer, length, और capacity copy करते हुए) जबकि compiler गारंटी देता है कि सिर्फ एक owner उस memory को उपयोग करता है।
उदाहरण: Why Rust Uses Moves Instead of Deep Copies
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `large_data`, set to `vec![1; 5]`
let large_data = vec![1; 5];
// Declare an immutable variable `transferred`, set to `large_data`
let transferred = large_data;
// Print "Transferred without deep copy: {:?}" to the console, substituting in `transferred` (with a trailing newline)
println!("Transferred without deep copy: {:?}", transferred);
}
Login to try C/C++/Java/PHP code in the editor
- किसी variable की value को किसी दूसरे variable में या किसी function में move करने के बाद इसे उपयोग करने की कोशिश करना, एक compile error का कारण बनते हुए।
- यह मान लेना कि assignment हमेशा data copy करता है जैसा कुछ दूसरी languages में हो सकता है -- heap types के लिए, assignment default रूप से move करता है।
- एक move को एक shallow copy के साथ confuse करना जो उसी data के दो usable references छोड़ता है -- Rust original को invalidate कर देता है।
- किसी non-
Copyvalue को किसी दूसरे variable को assign करना ownership move करता है; original variable invalid हो जाता है। - किसी function में एक non-
Copyvalue pass करना भी इसे move करता है, जब तक function एक reference न ले। - किसी moved-from variable को उपयोग करना एक compile-time error है, runtime bug नहीं।
- Move semantics data deep-copy करने की cost से बचते हैं जबकि अब भी use-after-free रोकते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: