Where items data stored

hello guys i want to check something
Where items like statics , consts , structs , functions ( i dont talk about function call like fun_identifier() i talk about function data itself like when i use function _identifier as expression without '( )' ) are they stored in stack or heap or static segment

because i can use any item before define it so i thought that items stored not in stack but in static before any main function instruction is executed

fn main () {
let x:i32 =Z; // i used Z before define it
static Z:i32 = 10 ;

Rust is not a one-pass compiler. Therefore the order of non-macro declarations and invocations is pretty loose. In general, data of fixed (i.e., compile-time computable) size is stored on the stack, whereas data of run-time computed size generally is stored on the heap

1 Like

Understand that Rust itself doesn't define what things are stored where. Rust code describes the behavior of a kind of Platonic ideal -- an imaginary computer. The term of art for this is abstract machine. Abstract machines don't really exist, they are a tool for defining languages. As long as you uphold the rules of Rust, the compiler promises to spit out a program for your real computer that does the same thing the Rust abstract machine would do, if one existed. (If you break the rules, the compiler's not obligated to do anything sensible -- which is the meaning of UB.)

The compiler can choose to write the real program in any way it wants, so long as it has the correct behavior. Since there is more than one way to write a program with any given behavior, it's not always possible to say what a given piece of Rust code will compile to; if that's important, you should rely on actually compiling it and looking at the assembly code. Compiler Explorer is a great resource for comparing small programs.

That said, there are some things we can observe about the behavior of compiled programs on typical platforms that Rust currently supports, such as:

  • statics are often, unsurprisingly, stored in static memory, as are functions.
  • consts are not necessarily stored anywhere (although they can be), but are "baked in" to the executable code. For example, if you write let x = y + 2; that may be compiled to a single instruction with an immediate argument. The 2 is part of the instruction.
  • structs (and enums) are not stored anywhere, because they are only a compile-time construct. The struct itself is "baked in" to the code in the form of pointer offsets and size multipliers and things like that. (Values of structure types may be stored anywhere.)

The compiler does a lot of analysis on Rust code before generating the code that will eventually be executed. It doesn't step through the source code line by line and emit a bunch of machine code for every line of Rust. In fact, many features, like structs, modules, and (to the surprise of many) lifetimes, emit no code at all.


thank you trentj :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.