DEV Community

[Advanced Rust] 1.6. Memory Part 4 - Static Memory and the 'static Lifetime Annotation

1.6.1. Static Memory

Static memory is actually a collective term. It refers to several closely related regions in the compiled program file. When the program runs, these regions are automatically loaded into memory. Values in static memory live for the entire duration of the program.

The program’s static memory contains the binary code of the program itself, which is usually mapped as read-only. As the program executes, it walks through the binary instructions in the text segment one by one, and jumps when a function is called.

Static memory holds the memory for variables declared with static, as well as some constant values, such as strings.

1.6.2. The 'static Lifetime Annotation

'static is a special lifetime, and its name comes from static memory. It marks a reference as valid for as long as static memory exists - that is, until the program exits. The memory for a static variable is allocated when the program starts. By definition, a reference to a value stored in static memory is 'static, because it will not be freed until the program ends.

However, a reference with the 'static lifetime annotation does not have to point to static memory. If a reference with the 'static lifetime annotation does not have to point to static memory, why is this lifetime called 'static? Isn’t it misleading if something has 'static but is not stored in static memory?

The name 'static still makes sense because: once you create a reference with the 'static lifetime, for the rest of the program it may as well point to static memory, because the program is allowed to use it for as long as it wants.

In other words, the name 'static may make people think that all references with the 'static lifetime point to static memory - that is, global variables or constants that live for the entire duration of the program. In reality, 'static only means that the reference is valid for the entire lifetime of the program; it does not require the referenced data to be stored in the static section.

Put differently, a reference with 'static means β€œthis reference can live forever, and the program can use it at any time”, but it does not force the underlying data to be statically allocated.

When writing Rust code, you will encounter the 'static lifetime annotation more often than static memory itself. 'static often appears in trait bounds for type parameters. For example, T: 'static means that type T can live for as long as we want - until the program exits - and it also means that T must be owned and self-sufficient. In other words, the type must either not borrow any other (non-static) values or only borrow values that are static. That guarantees the type can live until the end of the program.

1.6.3. The Difference Between const and static

The const keyword declares what follows it as a constant, for example:

const X: i32 = 123;

X is declared as a constant. Constants can be fully evaluated at compile time. During that process, any code that refers to the constant is replaced with the constant’s computed value. For example:

const X: i32 = 123;
println!("{}", X);

The print operation in this line will be rewritten at compile time as:

println!("{}", 123);

So a constant has no memory or associated storage (because it is not a place). You can think of a constant as a convenient name for a specific value.

Comments

No comments yet. Start the discussion.