a) What implicit lifetime does the &str
have, is it &'static str
or &'a str
or something else?
let a: &str = "hey";
b) The data "hey"
is in preallocated read-only memory.
1 | let mut x = "hey";
-> 3 bytes - UTF-8
What happens in memory(preallocated read-only) when the value changes?
2 | x = "world";
-> 5 bytes - UTF-8
- x will point to another memory location and will take 3 + 5 bytes in total? The program memory swells forever
- "hey" will be removed somehow from read-only memory, and "world" - will be written elsewhere - 5 bytes in total?
- "hey" will be overwritten by "world" and memory extended - 5 bytes total?
I wonder what type to use in a structure that will only be one, for the duration of the program. Structure values will change dynamically.
struct User {
username: String,
email: String,
}
or
struct User {
username: &'static str,
email: &'static str,
}
or
struct User<'a> {
username: &'a str,
email: &'a str,
}