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

Mutable References क्या हैं

एक mutable reference किसी को आपकी value borrow करने और असल में बदलने देता है, लेकिन एक समय में सिर्फ एक व्यक्ति के पास वह permission हो सकती है।
Syntax
rust
let mut value = data;
let r = &mut value;
*r = new_value;

fn name(param: &mut Type) {}

Creating a Mutable Reference

&mut value लिखना एक reference बनाता है जो इसके point किए गए data को modify करने की अनुमति है। Referenced किया जा रहा variable खुद mut से declare किया गया होना चाहिए।

उदाहरण: Creating a Mutable Reference

markup
// Define the function `add_exclamation` taking `s`
fn add_exclamation(s: &mut String) {
    s.push_str("!");
}

// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `message`, initialized to `String::from("Hello")`
    let mut message = String::from("Hello");
    add_exclamation(&mut message);
    // Print "{}" to the console, substituting in `message` (with a trailing newline)
    println!("{}", message);
}

Only One Mutable Reference at a Time

Rust enforce करता है कि किसी दिए scope के अंदर किसी value के ज़्यादा से ज़्यादा एक mutable reference मौजूद हो सकता है, code के दो हिस्सों को अचानक एक साथ वही data बदलने से रोकते हुए।

Note: Note करें r1 का scope r2 बनाए जाने से पहले खत्म होता है -- यही इसे compile कराता है।

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

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

Cannot Mix Mutable and Immutable References

आप एक ही value के किसी भी immutable references के साथ एक साथ एक mutable reference नहीं रख सकते, क्योंकि अन्यथा एक reader data को mid-read में अचानक बदलते हुए देख सकता है।

उदाहरण: Cannot Mix Mutable and Immutable References

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `value`, initialized to `10`
    let mut value = 10;
    {
        // Declare an immutable variable `readable`, set to `&value`
        let readable = &value;
        // Print "Read before mutation: {}" to the console, substituting in `readable` (with a trailing newline)
        println!("Read before mutation: {}", readable);
    }
    // Declare an immutable variable `writable`, set to `&mut value`
    let writable = &mut value;
    *writable += 5;
    // Print "After mutation: {}" to the console, substituting in `value` (with a trailing newline)
    println!("After mutation: {}", value);
}

Dereferencing to Modify

एक mutable reference के पीछे की value बदलने के लिए, underlying data को सीधे access करने के लिए dereference operator * उपयोग करें।

उदाहरण: Dereferencing to Modify

markup
// Define the function `double` taking `n`
fn double(n: &mut i32) {
    *n *= 2;
}

// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `x`, initialized to `7`
    let mut x = 7;
    double(&mut x);
    // Print "x is now {}" to the console, substituting in `x` (with a trailing newline)
    println!("x is now {}", x);
}
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. उसी scope में उसी value के दो mutable references बनाने की कोशिश करना, जिसे borrow checker मना करता है।
  2. एक ही value के लिए एक साथ एक mutable reference और एक immutable reference mix करना।
  3. एक mutable reference बनाते समय original variable और reference खुद दोनों पर mut भूल जाना।
चैप्टर सारांश
  • एक mutable reference &mut value लिखा जाता है और borrowed data modify करने देता है।
  • किसी particular value का एक समय में इसके scope के अंदर सिर्फ एक mutable reference मौजूद हो सकता है।
  • आपके पास एक ही समय में एक mutable reference और कोई immutable references active नहीं हो सकते।
  • Mutably borrow किए जाने से पहले original variable को खुद mut से declare किया जाना चाहिए।
🔒

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.