2 ms·
For anyone who does care, the answer is that String is not only mutable it's always heap allocated and can grow, whereas &str is just an immutable reference and
by tialaramex 4y ago
For anyone who does care, the answer is that String is not only mutable it's always heap allocated and can grow, whereas &str is just an immutable reference and so it could be pointing at the text in your String, but equally it could point to the stack, or your text segment, or a ROM.
C++ programmers might do OK to think of &str as like a C++ 17 string_view, except it's promised to always be UTF-8 text, and because it was provided from the outset in the language this type is always used where appropriate, so the string literal "Test" is a &'static str, and all the APIs you use work with &str and never some archaic pointer type.
Rust's String is a bit like C++ std::string, though deliberately lacking the Small String Optimisation but again always UTF-8, and String implements the Trait Deref<Target=str> which means most read-only methods are defined on &str but work fine on String anyway.
[edited: A handful of read-only methods make sense specifically for String, like asking how much space is allocated for the String, so those are in fact methods on String]