Lifetime Annotation का Syntax
'a, जो आपको compiler को बिल्कुल बताने देते हैं कि दो references age में कैसे related हैं।In this page:
&'a Type
&'a mut Type
fn name<'a>(param: &'a Type) -> &'a Type {}
Basic Lifetime Annotation Syntax
एक lifetime parameter, 'a जैसे एक apostrophe के बाद एक नाम से लिखा गया, बिल्कुल एक generic type parameter की तरह angle brackets में declared होता है, फिर signature में reference types annotate करने के लिए उपयोग होता है।
उदाहरण: Basic Lifetime Annotation Syntax
fn first<'a>(x: &'a str, _y: &str) -> &'a str {
x
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `a`, set to `String::from("first string")`
let a = String::from("first string");
// Declare an immutable variable `b`, set to `String::from("second string")`
let b = String::from("second string");
// Print "{}" to the console, substituting in `first(&a, &b)` (with a trailing newline)
println!("{}", first(&a, &b));
}
Login to try C/C++/Java/PHP code in the editor
Annotations Describe, Not Create, Relationships
कई parameters पर 'a लिखना उन्हें एक समान जीने पर मजबूर नहीं करता -- यह compiler को यह जांचने को कहता है कि returned reference input lifetimes में सबसे छोटे जितना कम से कम valid है।
उदाहरण: Annotations Describe, Not Create, Relationships
fn choose_first<'a>(x: &'a str, y: &'a str) -> &'a str {
// Check whether `!x.is_empty() { x } else { y }`
if !x.is_empty() { x } else { y }
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `one`, set to `String::from("one")`
let one = String::from("one");
// Declare an immutable variable `two`, set to `String::from("two")`
let two = String::from("two");
// Print "{}" to the console, substituting in `choose_first(&one, &two)` (with a trailing newline)
println!("{}", choose_first(&one, &two));
}
Login to try C/C++/Java/PHP code in the editor
Lifetime Annotations on Multiple References
जब दो parameters एक ही lifetime label share करते हैं, compiler function के return value जांचने के purpose के लिए उनकी validity को एक साथ जोड़ देता है।
उदाहरण: Lifetime Annotations on Multiple References
fn longer<'a>(a: &'a str, b: &'a str) -> &'a str {
// Check whether `a.len() >= b.len() { a } else { b }`
if a.len() >= b.len() { a } else { b }
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `x`, set to `String::from("hi")`
let x = String::from("hi");
// Declare an immutable variable `y`, set to `String::from("hello")`
let y = String::from("hello");
// Print "{}" to the console, substituting in `longer(&x, &y)` (with a trailing newline)
println!("{}", longer(&x, &y));
}
Login to try C/C++/Java/PHP code in the editor
Naming Convention for Lifetimes
Convention से, lifetime नाम छोटे और lowercase हैं, आमतौर पर 'a से शुरू होते हुए, फिर उसी signature में अतिरिक्त distinct lifetimes के लिए 'b, 'c वगैरह।
उदाहरण: Naming Convention for Lifetimes
fn pick_second<'a, 'b>(_first: &'a str, second: &'b str) -> &'b str {
second
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `one`, set to `String::from("one")`
let one = String::from("one");
// Declare an immutable variable `two`, set to `String::from("two")`
let two = String::from("two");
// Print "{}" to the console, substituting in `pick_second(&one, &two)` (with a trailing newline)
println!("{}", pick_second(&one, &two));
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि किसी lifetime annotation में
'aएक specific duration है -- यह सिर्फ एक generic label है जो compiler अलग-अलग references को relate करने के लिए उपयोग करता है। - यह भूल जाना कि lifetime parameters angle brackets में declared होते हैं, generic type parameters के similar, जैसे
fn foo<'a>(...)। - किसी ऐसे function में lifetime annotations जोड़ना जिसे असल में इनकी ज़रूरत नहीं, जब compiler खुद relationship infer कर सकता था।
- Lifetime parameters एक apostrophe से लिखे जाते हैं, जैसे
'a, और generics के साथ angle brackets में declared होते हैं। &'a strजैसा एक annotation यह नहीं बदलता कि data कितनी देर जीता है -- यह compiler के जांचने के लिए एक मौजूदा relationship describe करता है।- कई references एक ही lifetime parameter share कर सकते हैं जब उनकी validity एक साथ बंधी हो।
- Lifetime names convention से छोटे, lowercase letters होते हैं,
'a,'b, वगैरह से शुरू होते हुए।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: