Dangling References को रोकना
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
Login to try C/C++/Java/PHP code in the editor
The Fix: Return Owned Data
जब आप अन्यथा एक dangling reference बनाते, standard solution इसके बजाय एक owned value return करना है, ownership को function से बाहर transfer करते हुए ताकि data function call के बाद भी survive करे।
उदाहरण: The Fix: Return Owned Data
// 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);
}
Login to try C/C++/Java/PHP code in the editor
References Tied to Their Data's Lifetime
जब तक एक reference सिर्फ तब उपयोग होता है जब इसका underlying data अब भी जीवित है (अब भी scope में किसी के पास owned), यह पूरी तरह सुरक्षित है, और यही बिल्कुल वह है जो compiler आपके program में हर reference के लिए जांचता है।
उदाहरण: References Tied to Their Data's Lifetime
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- function के अंदर बनाए गए किसी local variable का reference return करने की कोशिश करना, जो return पर scope से बाहर चला जाता।
- यह मान लेना कि Rust C की तरह dangling pointers allow करता है, सिर्फ warnings के साथ -- Rust ऐसे code को compile करने से पूरी तरह मना कर देता है।
- एक 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: