What is the difference between static vs &'static str

Hello Rust Community,
What is the difference between static vs &'static str?

static is a keyword, &'static str is a type. Can you be more specific about your question?

EX:pub static BIOMES: [Option<(&'static str, f32, f32)>; 256]

for example what happens when I type static &'static what happens when I type str

pub static BIOMES defines a variable BIOMES that will exist for the whole duration of the program.

&'static str is the type of a reference to str (a string slice) that will never be deallocated.

A string literal such as "Hello" has the type &'static str.

You may want to read the Rust book for an introduction to these concepts.

3 Likes

The keyword static tells the compiler to create a variable that lives in the static memory of the program which means it exists during the whole runtime. The 'static in &'static str is a special lifetime name which marks that the referred object (in this case a str) will exist during the whole (remaining) runtime of the program.

3 Likes

I read the book but it's been a long time I forgot some things I will read it again

To me, explanations like:

are more clear and more memorable, than RustBook:

As a reference lifetime 'static indicates that the data pointed to by the reference lives for the entire lifetime of the running program. It can still be coerced to a shorter lifetime.

There are two ways to make a variable with 'static lifetime, and both are stored in the read-only memory of the binary:

  • Make a constant with the static declaration.
  • Make a string literal which has type: &'static str.

See the following example for a display of each method:

A static item is a value which is valid for the entire duration of your program (a 'static lifetime).

On the surface, static items seem very similar to consts: both contain a value, both require type annotations and both can only be initialized with constant functions and values. However, statics are notably different in that they represent a location in memory. That means that you can have references to static items and potentially even modify them, making them essentially global variables.

Static items do not call drop at the end of the program.

There are two types of static items: those declared in association with the mut keyword and those without.

Static items cannot be moved:

A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location. Static items have the static lifetime, which outlives all other lifetimes in a Rust program. Static items do not call drop at the end of the program.

The static initializer is a constant expression evaluated at compile time. Static initializers may refer to other statics.

Non-mut static items that contain a type that is not interior mutable may be placed in read-only memory.

All access to a static is safe, but there are a number of restrictions on statics:

  • The type must have the Sync trait bound to allow thread-safe access.
  • Constants cannot refer to statics.

The initializer expression must be omitted in an external block, and must be provided for free static items.

So, this forum is best RustBook. For beginners experienced users are best translators of RustBook. :upside_down_face:

1 Like

I agree with your opinion, I really learned a lot of important things about rust in this forum.

A little warning, Having a really large &'static variable could cause your program to run out of memory.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.