static

متغیرهای ایستا در طول عمر کل اجرای برنامه خواهند ماند و بنابراین منتقل نمی‌شوند:

static BANNER: &str = "Welcome to RustOS 3.14";

fn main() {
    println!("{BANNER}");
}

As noted in the Rust RFC Book, these are not inlined upon use and have an actual associated memory location. This is useful for unsafe and embedded code, and the variable lives through the entirety of the program execution. When a globally-scoped value does not have a reason to need object identity, const is generally preferred.

This slide should take about 5 minutes.
  • static is similar to mutable global variables in C++.
  • static هویت شی را فراهم می‌کند: آدرسی در حافظه و حالتی که توسط انواع با تغییرپذیری داخلی مانند Mutex<T> را نیاز دارد.

برای کاوش بیشتر

Because static variables are accessible from any thread, they must be Sync. Interior mutability is possible through a Mutex, atomic or similar.

Thread-local data can be created with the macro std::thread_local.