Rust by Example

20 Global constants

Constants can be declared in the global scope using the static keyword, the type annotation is obligatory in this case. These constants are placed in a read-only section of the memory and can be accessed in any other part of the program.

String literals like "string" can also be assigned to static variables. These variables have type signature &'static str, and are references to strings allocated in read-only memory. 'static is a special lifetime that outlives all the other lifetimes, and indicates that the referenced data is available in all the scopes.

static LANGUAGE: &'static str = "Rust"; static THRESHOLD: i32 = 10; fn is_big(n: i32) -> bool { // Access constant in some function n > THRESHOLD } fn main() { let n = 16; // Access constant in the main thread println!("This is {}", LANGUAGE); println!("The threshold is {}", THRESHOLD); println!("{} is {}", n, if is_big(n) { "big" } else { "small" }); // Error! Cannot modify a static item THRESHOLD = 5; // FIXME ^ Comment out this line { // String literals are references to read-only memory let _static_string: &'static str = "In read-only memory"; // When `_static_string` goes out of scope, we can no longer refer to // the underlying data, but the string remains in the read-only memory } }