Static Lifetime क्या है
'static उस data को चिह्नित करता है जो program की पूरी ज़िंदगी भर रहता है, कभी जल्दी गायब नहीं होता।In this page:
let name: &'static str = "text";
fn name<T: 'static>(param: T) {}
String Literals Are Static
Rust source code में हर string literal का अपने आप type &'static str होता है, क्योंकि यह सीधे compiled binary में embedded है और पूरे program की run के लिए मौजूद रहता है।
उदाहरण: String Literals Are Static
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `literal` (a `&'static str`), set to `"I live for the whole program"`
let literal: &'static str = "I live for the whole program";
// Print "{}" to the console, substituting in `literal` (with a trailing newline)
println!("{}", literal);
}
Login to try C/C++/Java/PHP code in the editor
Declaring a Static Variable
static keyword एक fixed memory location वाली एक global value declare करता है जो पूरे program तक चलती है, एक 'static reference से distinct लेकिन meaning में closely related।
उदाहरण: Declaring a Static Variable
static GREETING: &str = "Hello from a static variable";
// Entry point: execution of the program starts here
fn main() {
// Print "{}" to the console, substituting in `GREETING` (with a trailing newline)
println!("{}", GREETING);
}
Login to try C/C++/Java/PHP code in the editor
When You Might See 'static in Signatures
किसी generic parameter पर 'static bound मांगने वाला function कह रहा है कि value में कोई non-'static borrowed references नहीं होने चाहिए, जो long-term store होने वाली values के लिए आम है, जैसे किसी दूसरे thread में।
उदाहरण: When You Might See 'static in Signatures
fn print_forever<T: std::fmt::Display + 'static>(value: T) {
// Print "{}" to the console, substituting in `value` (with a trailing newline)
println!("{}", value);
}
// Entry point: execution of the program starts here
fn main() {
print_forever(String::from("owned, so it qualifies as 'static"));
}
Login to try C/C++/Java/PHP code in the editor
Avoid Overusing 'static
एक lifetime error चुप कराने के लिए 'static तक पहुंचना आमतौर पर एक संकेत है कि असली borrow relationship को अलग तरीके से express किया जाना चाहिए -- इसे सच में whole-program data describe करना चाहिए, एक generic escape hatch नहीं होना चाहिए।
उदाहरण: Avoid Overusing 'static
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `owned_instead_of_static`, set to `String::from("Prefer owning data over forcing 'static")`
let owned_instead_of_static = String::from("Prefer owning data over forcing 'static");
// Print "{}" to the console, substituting in `owned_instead_of_static` (with a trailing newline)
println!("{}", owned_instead_of_static);
}
Login to try C/C++/Java/PHP code in the editor
'staticको किसी lifetime error चुप कराने के quick fix के रूप में overuse करना, जब यह असल में ज़रूरी असली relationship reflect न करे।- यह मान लेना कि हर string literal को एक explicit
'staticannotation चाहिए -- उनके पास पहले से implicitly यह है। 'staticdata को सिर्फ 'लंबे समय' तक जीने वाले data के साथ confuse करना -- इसका specifically मतलब है पूरे program की अवधि के लिए valid।
'staticएक special lifetime है जिसका मतलब है reference पूरे program की अवधि के लिए valid है।- String literals का अपने आप type
&'static strहोता है, क्योंकि वे सीधे compiled binary में embedded हैं। 'staticको जानबूझकर उपयोग किया जाना चाहिए, lifetime errors के लिए एक generic workaround के रूप में नहीं।- Data को जानबूझकर memory leak करके (दुर्लभ) या बस एक compile-time constant या literal होकर
'staticबनाया जा सकता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: