Ownership के Rules
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Scope and Automatic Cleanup
जब एक owning variable scope से बाहर जाता है, Rust अपने आप इसकी memory free करने के लिए cleanup code call करता है। यह बिना किसी garbage collector के deterministically होता है, बिल्कुल तब जब इसके scope का closing brace आ जाता है।
उदाहरण: Scope and Automatic Cleanup
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Only One Owner at a Time
चूंकि एक value का सिर्फ एक owner हो सकता है, इसे किसी दूसरे variable को assign करना ownership transfer (move) करता है बजाय एक दूसरा independent owner बनाने के। यह दो variables को एक ही memory free करने की कोशिश करने से रोकता है।
उदाहरण: Only One Owner at a Time
fn main() {
let a = String::from("hello");
let b = a; // ownership moves from a to b
println!("b owns the value: {}", b);
}
Login to try C/C++/Java/PHP code in the editor
Why This Matters
ये नियम bugs की पूरी categories खत्म करते हैं -- जैसे double frees और use-after-free -- जो manual memory management वाली languages में आम हैं, बिना runtime पर किसी garbage collector की ज़रूरत के।
उदाहरण: Why This Matters
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि एक value के एक साथ दो independent owners हो सकते हैं -- Rust का core नियम बिल्कुल एक समय में एक owner है।
- किसी variable की value कहीं और move होने के बाद इसे उपयोग करने की कोशिश करना, जिसे compiler compile time पर reject करता है।
- यह सोचना कि ownership rules सिर्फ
Stringजैसे heap-allocated types पर लागू होते हैं -- ये सभी values पर लागू होते हैं, हालांकि simple typesCopyभी हैं।
- 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: