Borrowing के Rules
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Why the Rule Exists
यह rule compile time पर data races रोकता है: अगर code किसी value को पढ़ रहा होता जबकि दूसरा code इसे एक साथ बदल रहा होता, reader असंगत या corrupted data देख सकता था। Rust बस उस possibility को disallow करता है।
उदाहरण: Why the Rule Exists
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Non-Lexical Lifetimes
Modern Rust एक reference के borrow को इसके आखिरी उपयोग होते ही खत्म होने देता है, सिर्फ इसके enclosing block के आखिर में नहीं, कई पहले-reject किए पैटर्न्स को compile कराते हुए।
उदाहरण: Non-Lexical Lifetimes
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);
}
Login to try C/C++/Java/PHP code in the editor
References Must Always Be Valid
Borrow checker यह भी गारंटी देता है कि एक reference कभी उस data से ज़्यादा जी नहीं सकता जिस पर यह point करता है, compile time पर dangling references को पूरी तरह खत्म करते हुए।
उदाहरण: References Must Always Be Valid
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- एक immutable reference को जीवित रखने की कोशिश करना जबकि साथ ही उसी data का एक mutable reference भी बना रहे हों।
- यह मान लेना कि borrow checker errors का मतलब है code मूल रूप से गलत है, जब अक्सर सिर्फ reordering या scope छोटा करना इसे ठीक कर देता है।
- यह भूल जाना कि 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: