Functions में Lifetimes
In this page:
fn name<'a>(x: &'a str, y: &'a str) -> &'a str {
// body
}
Returning a Reference Tied to a Parameter
जब कोई function दो reference parameters में से एक return कर सकता है, एक explicit lifetime annotation compiler को बताता है कि returned reference तब तक valid है जब तक दोनों inputs हैं।
उदाहरण: Returning a Reference Tied to a Parameter
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
// Check whether `x.len() > y.len() { x } else { y }`
if x.len() > y.len() { x } else { y }
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `s1`, set to `String::from("short")`
let s1 = String::from("short");
// Declare an immutable variable `s2`, set to `String::from("a bit longer")`
let s2 = String::from("a bit longer");
// Print "{}" to the console, substituting in `longest(&s1, &s2)` (with a trailing newline)
println!("{}", longest(&s1, &s2));
}
Login to try C/C++/Java/PHP code in the editor
Lifetime Elision for Simple Functions
बिल्कुल एक reference parameter और एक reference return type वाले functions के लिए, Rust के elision rules lifetime अपने आप infer करते हैं, इसलिए कोई explicit annotation की ज़रूरत नहीं।
उदाहरण: Lifetime Elision for Simple Functions
// Define the function `first_char` taking `s` and returning a string slice
fn first_char(s: &str) -> &str {
&s[0..1]
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `word`, set to `"Rust"`
let word = "Rust";
// Print "{}" to the console, substituting in `first_char(word)` (with a trailing newline)
println!("{}", first_char(word));
}
Login to try C/C++/Java/PHP code in the editor
Why You Cannot Return a Reference to a Local Variable
कोई function कभी validly locally बनाए data का reference return नहीं कर सकता, क्योंकि वह data function return होने पर drop हो जाता है; fix इसके बजाय एक owned value return करना है।
उदाहरण: Why You Cannot Return a Reference to a Local Variable
// Define the function `build_owned` with no parameters and returning a String
fn build_owned() -> String {
// Declare an immutable variable `local`, set to `String::from("owned and returned safely")`
let local = String::from("owned and returned safely");
local
}
// Entry point: execution of the program starts here
fn main() {
// Print "{}" to the console, substituting in `build_owned()` (with a trailing newline)
println!("{}", build_owned());
}
Login to try C/C++/Java/PHP code in the editor
Functions with Independent Lifetimes
जब किसी function का return value कई reference parameters में से सिर्फ एक से related हो, हर parameter को अपना independent lifetime देना उस relationship को सटीक रूप से clarify करता है।
उदाहरण: Functions with Independent Lifetimes
fn describe<'a>(name: &'a str, _ignored: &str) -> &'a str {
name
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `name`, set to `String::from("Rustacean")`
let name = String::from("Rustacean");
// Declare an immutable variable `other`, set to `String::from("unrelated")`
let other = String::from("unrelated");
// Print "{}" to the console, substituting in `describe(&name, &other)` (with a trailing newline)
println!("{}", describe(&name, &other));
}
Login to try C/C++/Java/PHP code in the editor
- किसी function से बिना किसी lifetime annotation के एक reference return करने की कोशिश करना जब compiler यह infer नहीं कर सकता कि यह किस input से related है।
- यह मान लेना कि कोई function पूरी तरह खुद के अंदर बनाए data का reference return कर सकता है -- वह data function call से ज़्यादा नहीं जीता।
- यह भूल जाना कि lifetime elision rules पहले से कई आम single-reference-parameter functions को अपने आप cover करते हैं।
- एक function जो एक reference return करता है उसे specify करना चाहिए कि return value किस parameter के lifetime से tied है।
- Lifetime elision rules compiler को कई simple, आम function shapes के लिए अपने आप lifetimes infer करने देते हैं।
- कोई function कभी विशुद्ध रूप से local data का reference return नहीं कर सकता -- इसे एक owned value या किसी parameter से tied reference return करना चाहिए।
- जब किसी function में कई reference parameters हों और यह उनमें से एक conditionally return करता है, आमतौर पर एक explicit lifetime ज़रूरी है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: