Untitled

Anonymous
Plain Text
08/20/2025 10:04 AM
591 B
12
Indexable
Never
// Define static variables
static A: i32 = 3;
static B: i64 = -1_000_000;

fn print_statics(from: &str) {
    println!("{from}: Static A = {A}, Address: {:p}", &A);
    println!("{from}: Static B = {B}, Address: {:p}", &B);
}

fn main() {
    println!("Static Allocation Example");

    // Print in main()
    print_statics("From main()");

    // Print in another function
    another_function();

    // If you try to reassign A or B, it fails for safety:
    // A = 42; // error: cannot assign to immutable static
}

fn another_function() {
    print_statics("From another function()");
}
Editor is loading...
Leave a Comment