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

Borrowing के Rules

Rust में values share करने के बारे में सख्त नियम हैं ताकि कोई गलती से कुछ पढ़ न ले जबकि कोई और इसे बदल रहा हो।
Syntax
rust
let r1 = &value;      // any number of shared refs
let r2 = &value;
// OR exactly one mutable ref
let r3 = &mut value;

The Core Borrowing Rule

Rust का borrowing rule है: किसी भी समय, आपके पास data के किसी particular टुकड़े का या तो बिल्कुल एक mutable reference हो सकता है, या कितने भी immutable references -- कभी दोनों तरह के एक साथ नहीं।

उदाहरण: The Core Borrowing Rule

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];
    // Declare an immutable variable `r1`, set to `&data`
    let r1 = &data;
    // Declare an immutable variable `r2`, set to `&data`
    let r2 = &data;
    // Print "{:?} and {:?}" to the console, substituting in `r1, r2` (with a trailing newline)
    println!("{:?} and {:?}", r1, r2);
}

Why the Rule Exists

यह rule compile time पर data races रोकता है: अगर code किसी value को पढ़ रहा होता जबकि दूसरा code इसे एक साथ बदल रहा होता, reader असंगत या corrupted data देख सकता था। Rust बस उस possibility को disallow करता है।

उदाहरण: Why the Rule Exists

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `counter`, initialized to `0`
    let mut counter = 0;
    {
        // Declare an immutable variable `r`, set to `&mut counter`
        let r = &mut counter;
        *r += 1;
    }
    // Print "counter = {}" to the console, substituting in `counter` (with a trailing newline)
    println!("counter = {}", counter);
}

Non-Lexical Lifetimes

Modern Rust एक reference के borrow को इसके आखिरी उपयोग होते ही खत्म होने देता है, सिर्फ इसके enclosing block के आखिर में नहीं, कई पहले-reject किए पैटर्न्स को compile कराते हुए।

उदाहरण: Non-Lexical Lifetimes

markup
fn main() {
    let mut value = 5;
    let r1 = &value;
    println!("r1: {}", r1); // last use of r1
    let r2 = &mut value; // fine: r1's borrow has ended
    *r2 += 1;
    println!("value: {}", value);
}

References Must Always Be Valid

Borrow checker यह भी गारंटी देता है कि एक reference कभी उस data से ज़्यादा जी नहीं सकता जिस पर यह point करता है, compile time पर dangling references को पूरी तरह खत्म करते हुए।

उदाहरण: References Must Always Be Valid

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `owner`, set to `String::from("valid data")`
    let owner = String::from("valid data");
    // Declare an immutable variable `borrowed`, set to `&owner`
    let borrowed = &owner;
    // Print "{}" to the console, substituting in `borrowed` (with a trailing newline)
    println!("{}", borrowed);
    // Print "owner is still around: {}" to the console, substituting in `owner` (with a trailing newline)
    println!("owner is still around: {}", owner);
}
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. एक immutable reference को जीवित रखने की कोशिश करना जबकि साथ ही उसी data का एक mutable reference भी बना रहे हों।
  2. यह मान लेना कि borrow checker errors का मतलब है code मूल रूप से गलत है, जब अक्सर सिर्फ reordering या scope छोटा करना इसे ठीक कर देता है।
  3. यह भूल जाना कि Non-Lexical Lifetimes एक reference के effective scope को इसके आखिरी उपयोग पर खत्म होने देते हैं, enclosing block के आखिर में नहीं।
चैप्टर सारांश
  • किसी भी समय, आपके पास या तो एक mutable reference, या कितने भी immutable references हो सकते हैं, लेकिन दोनों नहीं।
  • References को हमेशा valid होना चाहिए (उस data की ओर point करना जो drop नहीं हुआ) -- यह compile time पर जांचा जाता है।
  • Borrow checker इन नियमों को statically enforce करता है, कोई runtime cost नहीं जोड़ते हुए।
  • Non-Lexical Lifetimes का मतलब है एक reference का borrow इसके आखिरी असली उपयोग पर खत्म होता है, ज़रूरी नहीं इसके block के आखिर में।
🔒

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.