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

Move Semantics क्या है

जब आप Rust में किसी और को एक value देते हैं, तो आप अपनी पुरानी copy अब और उपयोग नहीं कर सकते -- यह असल में move हो गई।
Syntax
rust
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

markup
// 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);
}

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

markup
// 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");
}

Getting a Value Back by Returning It

एक function किसी value की ownership वापस caller को return कर सकता है, call के बाद आपको इसे एक (संभवतः नए) variable नाम के तहत उपयोग करते रहने देते हुए।

उदाहरण: Getting a Value Back by Returning It

markup
// 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);
}

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

markup
// 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);
}
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. किसी variable की value को किसी दूसरे variable में या किसी function में move करने के बाद इसे उपयोग करने की कोशिश करना, एक compile error का कारण बनते हुए।
  2. यह मान लेना कि assignment हमेशा data copy करता है जैसा कुछ दूसरी languages में हो सकता है -- heap types के लिए, assignment default रूप से move करता है।
  3. एक move को एक shallow copy के साथ confuse करना जो उसी data के दो usable references छोड़ता है -- Rust original को invalidate कर देता है।
चैप्टर सारांश
  • किसी non-Copy value को किसी दूसरे variable को assign करना ownership move करता है; original variable invalid हो जाता है।
  • किसी function में एक non-Copy value 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:

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.