Where &'a str is allocated?

⚓ Rust    📅 2025-09-10    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 64 days ago. The information described in this article may have changed.

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Where &'a str is allocated?

Please correct my understanding if I am wrong

&'static str = "hello" -> the hello is stored in read only data section (rodata, inside the binary directly) and then the variable contains pointer and length that point to said rodata

&str = String::from("hello").as_str() -> the hello is stored in heap and the variable contains pointer and length that point to said heap

let a = [b"hello 1", b"hello 2"]
&str = std::str::from_utf8(&buf) -> a is stored in stack, the str variable contains pointer and length that point to said stack

Now how about
&'a str = "hello" ?

Aka it is a string literal but the lifetime is limited, the lifetime is a not static.

Is the actual data stored in read only data section (rodata) or stack? And then only the view (pointer and length) that has lifetime a?

Or both the actual data and the view (pointer and length) are stored in stack with lifetime a?

1 post - 1 participant

Read full topic

🏷️ Rust_feed