4 ms·
It's like in C. "Ferris" is a pointer (and length, in Rust) into the binary's static storage segment. String::from("Ferris") creates a pointer to a string on t
by msbarnett 10y ago
It's like in C. "Ferris" is a pointer (and length, in Rust) into the binary's static storage segment.
String::from("Ferris") creates a pointer to a string on the heap with the contents "Ferris".
The difference is that in C this distinction is implicit, so you can accidentally try to modify the string in static storage, which is undefined behaviour, whereas Rust treats these distinct things as distinct types.
- panic 10y agoThis makes C's approach sound worse than it really is: in practice you'll either get a compiler warning or a segfault, not the potential nose-demons typically associated with undefined behavior. C also gives you a type-level way to track static strings (the const qualifier) though it isn't as strictly enforced.