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

Ownership के Rules

Ownership Rust का नियम है कि data के हर टुकड़े का बिल्कुल एक owner होता है जो इसे साफ़ करने के लिए ज़िम्मेदार है।
Syntax
rust
let owner = value;   // each value has one owner
let other = owner;   // ownership moves
// value is dropped when its owner goes out of scope

The Three Ownership Rules

Rust का ownership system तीन नियमों पर टिका है: हर value का एक owner है, एक समय में सिर्फ एक owner हो सकता है, और जब owner scope से बाहर जाता है, value drop हो जाती है। ये नियम पूरी तरह compile time पर enforce होते हैं।

उदाहरण: The Three Ownership Rules

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `owner`, set to `String::from("Rust")`
    let owner = String::from("Rust");
    // Print "{} is owned by this variable" to the console, substituting in `owner` (with a trailing newline)
    println!("{} is owned by this variable", owner);
}

Scope and Automatic Cleanup

जब एक owning variable scope से बाहर जाता है, Rust अपने आप इसकी memory free करने के लिए cleanup code call करता है। यह बिना किसी garbage collector के deterministically होता है, बिल्कुल तब जब इसके scope का closing brace आ जाता है।

उदाहरण: Scope and Automatic Cleanup

markup
// Entry point: execution of the program starts here
fn main() {
    {
        // Declare an immutable variable `temp`, set to `String::from("temporary")`
        let temp = String::from("temporary");
        // Print "Inside scope: {}" to the console, substituting in `temp` (with a trailing newline)
        println!("Inside scope: {}", temp);
    }
    // Print "temp has been dropped by now" to the console (with a trailing newline)
    println!("temp has been dropped by now");
}

Only One Owner at a Time

चूंकि एक value का सिर्फ एक owner हो सकता है, इसे किसी दूसरे variable को assign करना ownership transfer (move) करता है बजाय एक दूसरा independent owner बनाने के। यह दो variables को एक ही memory free करने की कोशिश करने से रोकता है।

उदाहरण: Only One Owner at a Time

markup
fn main() {
    let a = String::from("hello");
    let b = a; // ownership moves from a to b
    println!("b owns the value: {}", b);
}

Why This Matters

ये नियम bugs की पूरी categories खत्म करते हैं -- जैसे double frees और use-after-free -- जो manual memory management वाली languages में आम हैं, बिना runtime पर किसी garbage collector की ज़रूरत के।

उदाहरण: Why This Matters

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `data`, set to `vec![1, 2, 3]`
    let data = vec![1, 2, 3];
    // Print "data is owned here and will be cleaned up automatically: {:?}" to the console, substituting in `data` (with a trailing newline)
    println!("data is owned here and will be cleaned up automatically: {:?}", data);
}
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 के एक साथ दो independent owners हो सकते हैं -- Rust का core नियम बिल्कुल एक समय में एक owner है।
  2. किसी variable की value कहीं और move होने के बाद इसे उपयोग करने की कोशिश करना, जिसे compiler compile time पर reject करता है।
  3. यह सोचना कि ownership rules सिर्फ String जैसे heap-allocated types पर लागू होते हैं -- ये सभी values पर लागू होते हैं, हालांकि simple types Copy भी हैं।
चैप्टर सारांश
  • Rust में हर value का किसी भी समय बिल्कुल एक owner variable होता है।
  • जब owner scope से बाहर जाता है, Rust अपने आप value drop (free) कर देता है।
  • Ownership किसी दूसरे variable में transfer (move) की जा सकती है, जिसके बाद original binding और valid नहीं रहती।
  • ये नियम पूरी तरह compile time पर जांचे जाते हैं, शून्य runtime overhead के साथ।
🔒

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.