← Back to Rust Course | Chapter 12: Lifetimes | Lesson 2 of 6

Lifetime Annotation का Syntax

Lifetime annotations छोटे labels हैं, जैसे 'a, जो आपको compiler को बिल्कुल बताने देते हैं कि दो references age में कैसे related हैं।
Syntax
rust
&'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

markup
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));
}

Annotations Describe, Not Create, Relationships

कई parameters पर 'a लिखना उन्हें एक समान जीने पर मजबूर नहीं करता -- यह compiler को यह जांचने को कहता है कि returned reference input lifetimes में सबसे छोटे जितना कम से कम valid है।

उदाहरण: Annotations Describe, Not Create, Relationships

markup
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));
}

Lifetime Annotations on Multiple References

जब दो parameters एक ही lifetime label share करते हैं, compiler function के return value जांचने के purpose के लिए उनकी validity को एक साथ जोड़ देता है।

उदाहरण: Lifetime Annotations on Multiple References

markup
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));
}

Naming Convention for Lifetimes

Convention से, lifetime नाम छोटे और lowercase हैं, आमतौर पर 'a से शुरू होते हुए, फिर उसी signature में अतिरिक्त distinct lifetimes के लिए 'b, 'c वगैरह।

उदाहरण: Naming Convention for Lifetimes

markup
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));
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि किसी lifetime annotation में 'a एक specific duration है -- यह सिर्फ एक generic label है जो compiler अलग-अलग references को relate करने के लिए उपयोग करता है।
  2. यह भूल जाना कि lifetime parameters angle brackets में declared होते हैं, generic type parameters के similar, जैसे fn foo<'a>(...)।
  3. किसी ऐसे 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.