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

Lifetimes का परिचय

एक lifetime track करने का Rust का तरीका है कि एक borrowed value असल में कितनी देर के लिए अच्छी है, ताकि आप इसे कभी इसके जाने के बाद उपयोग न करें।
Syntax
rust
fn function_name<'a>(param1: &'a Type, param2: &'a Type) -> &'a Type {
    // return one of the references
}

What a Lifetime Represents

एक lifetime compiler का internal tracking है कि एक particular reference कितनी देर valid रहता है, यह सुनिश्चित करते हुए कि यह कभी उस data से ज़्यादा न जिए जिस पर यह point करता है।

उदाहरण: What a Lifetime Represents

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `text`, set to `String::from("valid data")`
    let text = String::from("valid data");
    // Declare an immutable variable `reference`, set to `&text`
    let reference = &text;
    // Print "{}" to the console, substituting in `reference` (with a trailing newline)
    println!("{}", reference);
}

Most Lifetimes Are Inferred

ज़्यादातर code में, compiler बिना आपके कोई lifetime syntax लिखे अपने आप lifetime relationships infer करता है।

उदाहरण: Most Lifetimes Are Inferred

markup
// Define the function `first_word` taking `s` and returning a string slice
fn first_word(s: &str) -> &str {
    // Match on `s.find(' ')` and branch by pattern
    match s.find(' ') {
        Some(i) => &s[..i],
        None => s,
    }
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `sentence`, set to `String::from("hello world")`
    let sentence = String::from("hello world");
    // Print "{}" to the console, substituting in `first_word(&sentence)` (with a trailing newline)
    println!("{}", first_word(&sentence));
}

Lifetimes Prevent Dangling References

Lifetime tracking का पूरा purpose गारंटी देना है कि एक reference कभी उस data के free होने के बाद उपयोग नहीं हो सकता जिस पर यह point करता है, mistake को बाद में crash करने के बजाय compile time पर पकड़ते हुए।

उदाहरण: Lifetimes Prevent Dangling References

markup
fn longest_of_two<'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 `a`, set to `String::from("short")`
    let a = String::from("short");
    // Declare an immutable variable `b`, set to `String::from("much longer string")`
    let b = String::from("much longer string");
    // Print "{}" to the console, substituting in `longest_of_two(&a, &b)` (with a trailing newline)
    println!("{}", longest_of_two(&a, &b));
}

No Runtime Cost

Lifetime checking पूरी तरह compilation के दौरान होती है। एक बार code सफलतापूर्वक compile हो जाए, compiled binary में कोई lifetime information नहीं बचती, और कोई runtime overhead नहीं।

उदाहरण: No Runtime Cost

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `value`, set to `100`
    let value = 100;
    // Declare an immutable variable `borrowed`, set to `&value`
    let borrowed = &value;
    // Print "Borrowed with zero runtime cost: {}" to the console, substituting in `borrowed` (with a trailing newline)
    println!("Borrowed with zero runtime cost: {}", borrowed);
}
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. यह मान लेना कि lifetimes control करते हैं कि data कितनी देर जीता है -- वे सिर्फ पहले से मौजूद relationships describe करते हैं, वे कुछ भी extend नहीं करते।
  2. यह सोचना कि हर reference को एक explicit lifetime annotation चाहिए -- ज़्यादातर cases बिना किसी के अपने आप infer हो जाते हैं।
  3. यह मानना कि lifetimes एक runtime feature हैं -- वे विशुद्ध रूप से एक compile-time concept हैं शून्य runtime cost के साथ।
चैप्टर सारांश
  • एक lifetime describe करता है कि एक reference उस data के सापेक्ष कितनी देर valid रहता है जिस पर यह point करता है।
  • ज़्यादातर lifetimes compiler द्वारा अपने आप infer होते हैं और इन्हें कभी explicitly लिखने की ज़रूरत नहीं।
  • Lifetimes पूरी तरह compile time पर dangling references रोकने के लिए मौजूद हैं।
  • Explicit lifetime annotations ज़रूरी हो जाते हैं जब compiler खुद से relationship infer नहीं कर सकता।
🔒

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.