Why does Rust have String
and str
? What are the differences between String
and str
? When does one use String
I have a C++ background and I found it very useful to think about String
and &str
in C++ terms:
String
is like a std::string
; it owns the memory and does the dirty job of managing memory.&str
is like a char*
(but a little more sophisticated); it points us to the beginning of a chunk in the same way you can get a pointer to the contents of std::string
.Are either of them going to disappear? I do not think so. They serve two purposes:
String
keeps the buffer and is very practical to use. &str
is lightweight and should be used to "look" into strings. You can search, split, parse, and even replace chunks without needing to allocate new memory.
&str
can look inside of a String
as it can point to some string literal. The following code needs to copy the literal string into the String
managed memory:
let a: String = "hello rust".into();
The following code lets you use the literal itself without copy (read only though)
let a: &str = "hello rust";