Lifetimes का परिचय
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Most Lifetimes Are Inferred
ज़्यादातर code में, compiler बिना आपके कोई lifetime syntax लिखे अपने आप lifetime relationships infer करता है।
उदाहरण: Most Lifetimes Are Inferred
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Lifetimes Prevent Dangling References
Lifetime tracking का पूरा purpose गारंटी देना है कि एक reference कभी उस data के free होने के बाद उपयोग नहीं हो सकता जिस पर यह point करता है, mistake को बाद में crash करने के बजाय compile time पर पकड़ते हुए।
उदाहरण: Lifetimes Prevent Dangling References
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));
}
Login to try C/C++/Java/PHP code in the editor
No Runtime Cost
Lifetime checking पूरी तरह compilation के दौरान होती है। एक बार code सफलतापूर्वक compile हो जाए, compiled binary में कोई lifetime information नहीं बचती, और कोई runtime overhead नहीं।
उदाहरण: No Runtime Cost
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि lifetimes control करते हैं कि data कितनी देर जीता है -- वे सिर्फ पहले से मौजूद relationships describe करते हैं, वे कुछ भी extend नहीं करते।
- यह सोचना कि हर reference को एक explicit lifetime annotation चाहिए -- ज़्यादातर cases बिना किसी के अपने आप infer हो जाते हैं।
- यह मानना कि 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: