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

Static Lifetime क्या है

'static उस data को चिह्नित करता है जो program की पूरी ज़िंदगी भर रहता है, कभी जल्दी गायब नहीं होता।
Syntax
rust
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

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

Declaring a Static Variable

static keyword एक fixed memory location वाली एक global value declare करता है जो पूरे program तक चलती है, एक 'static reference से distinct लेकिन meaning में closely related।

उदाहरण: Declaring a Static Variable

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

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

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

Avoid Overusing 'static

एक lifetime error चुप कराने के लिए 'static तक पहुंचना आमतौर पर एक संकेत है कि असली borrow relationship को अलग तरीके से express किया जाना चाहिए -- इसे सच में whole-program data describe करना चाहिए, एक generic escape hatch नहीं होना चाहिए।

उदाहरण: Avoid Overusing 'static

markup
// 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);
}
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. 'static को किसी lifetime error चुप कराने के quick fix के रूप में overuse करना, जब यह असल में ज़रूरी असली relationship reflect न करे।
  2. यह मान लेना कि हर string literal को एक explicit 'static annotation चाहिए -- उनके पास पहले से implicitly यह है।
  3. 'static data को सिर्फ 'लंबे समय' तक जीने वाले 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:

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.