String vs str
Rust terminology:
- an immutable reference to a string slice.
String
a mutable string buffer.Rust’s
String
type is a wrapper around a vector of bytes. As with aVec<T>
, it is owned.As with many other types creates a string from a string literal;
String::new()
creates a new empty string, to which string data can be added using thepush()
andpush_str()
methods.You can borrow slices from
String
via&
and optionally range selection.For C++ programmers: think of
&str
asconst char*
from C++, but the one that always points to a valid string in memory. RustString
is a rough equivalent of from C++ (main difference: it can only contain UTF-8 encoded bytes and will never use a small-string optimization).