← Back to Rust Course | Chapter 6: Borrowing & References | Lesson 4 of 6

Dangling References को रोकना

Rust सुनिश्चित करता है कि आप कभी एक ऐसा note पकड़े न रह जाएं जो किसी ऐसी चीज़ की ओर point करता हो जो अब मौजूद नहीं है।
Syntax
rust
fn name() -> Type {
    let value = data;
    value  // return the owned value, not &value
}

What a Dangling Reference Would Be

एक dangling reference उस memory की ओर point करता है जो पहले ही free हो चुकी है। C जैसी languages में, यह चुपचाप undefined behavior का कारण बन सकता है; Rust का compiler ऐसे code को इसके चलने से पहले ही पकड़ता और reject करता है।

उदाहरण: What a Dangling Reference Would Be

markup
// Define the function `valid_reference` with no parameters and returning a String
fn valid_reference() -> String {
    // Declare an immutable variable `s`, set to `String::from("owned, not dangling")`
    let s = String::from("owned, not dangling");
    s
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `result`, set to `valid_reference()`
    let result = valid_reference();
    // Print "{}" to the console, substituting in `result` (with a trailing newline)
    println!("{}", result);
}

Why Returning a Reference to a Local Fails

अगर कोई function locally बनाए गए किसी variable का एक reference return करने की कोशिश करता, वह variable function खत्म होने पर drop हो जाता, reference को free memory की ओर point करते हुए छोड़ते हुए -- इसलिए Rust का compiler इसे compile करने से मना कर देता है।

उदाहरण: Why Returning a Reference to a Local Fails

markup
fn safe_owned_return() -> String {
    let local = String::from("built locally");
    local // returning ownership, not a reference, is safe
}

fn main() {
    let value = safe_owned_return();
    println!("{}", value);
}

The Fix: Return Owned Data

जब आप अन्यथा एक dangling reference बनाते, standard solution इसके बजाय एक owned value return करना है, ownership को function से बाहर transfer करते हुए ताकि data function call के बाद भी survive करे।

उदाहरण: The Fix: Return Owned Data

markup
// Define the function `build_message` taking `name` and returning a String
fn build_message(name: &str) -> String {
    format!("Hello, {}!", name)
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `message`, set to `build_message("Rust")`
    let message = build_message("Rust");
    // Print "{}" to the console, substituting in `message` (with a trailing newline)
    println!("{}", message);
}

References Tied to Their Data's Lifetime

जब तक एक reference सिर्फ तब उपयोग होता है जब इसका underlying data अब भी जीवित है (अब भी scope में किसी के पास owned), यह पूरी तरह सुरक्षित है, और यही बिल्कुल वह है जो compiler आपके program में हर reference के लिए जांचता है।

उदाहरण: References Tied to Their Data's Lifetime

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `text`, set to `String::from("alive for the whole scope")`
    let text = String::from("alive for the whole scope");
    // Declare an immutable variable `reference`, set to `&text`
    let reference = &text;
    // Print "{}" to the console, substituting in `reference` (with a trailing newline)
    println!("{}", reference);
}
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. function के अंदर बनाए गए किसी local variable का reference return करने की कोशिश करना, जो return पर scope से बाहर चला जाता।
  2. यह मान लेना कि Rust C की तरह dangling pointers allow करता है, सिर्फ warnings के साथ -- Rust ऐसे code को compile करने से पूरी तरह मना कर देता है।
  3. एक dangling reference error ठीक करने के लिए एक owned value return करना, लेकिन function का return type update करना भूल जाना।
चैप्टर सारांश
  • एक dangling reference उस memory की ओर point करता है जो पहले ही free हो चुकी है; Rust का compiler ऐसे code को पूरी तरह reject करता है।
  • Borrow checker track करता है कि data कितनी देर जीवित रहता है (इसका lifetime) और यह सुनिश्चित करता है कि references कभी अपने data से ज़्यादा न जिएं।
  • एक would-be dangling reference के लिए typical fix reference के बजाय एक owned value return करना है।
  • यह गारंटी पूरी तरह compile time पर enforce होती है, किसी runtime checks की ज़रूरत के बिना।
🔒

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.