There are two different string-like types in Rust.
String
is the type of owned strings. If you have a String
, you have total ownership over it. You can modify it, append to it, empty it, and, yes, even drop it. You can pass ownership to someone else, you can let someone else borrow it, and so on. If you take a reference to a String
, you get a &String
(an immutable reference to a String
). This type is fairly useless, and most style checkers will warn you about that. On the other hand, the mutable reference of String
, denoted &mut String
, can be useful since the borrower can append to the string without taking ownership of it.
str
is a string slice. It's an unsized type, so it can't be used as a function argument or a structure component without being wrapped in a reference or a Box
. Most commonly, you see it in the form &str
, which is an immutably-borrowed string slice. Since this is a reference, it's sized and thus can be used in most conventional contexts.
String::new
returns, as the name implies, a new owned String
. A string literal such as ""
has type &'static str
, i.e. a borrowed string slice with static lifetime.
You can borrow a String
as a &str
with as_str
, or with the Deref
instance for String
. The latter will often kick in automatically if you try to call a str
method on a String
. On the other hand, there's no way to treat a &str
as a String
, since the former is borrowed and the latter implies ownership, a stronger guarantee. If you want to go the other way, you need to copy the data from the &str
into a new String
. This can be done in a couple of ways, simply by using different traits.
String::from(my_str)
my_str.to_owned()
Personally, I use String::from
on string literals (i.e. String::from("some constant")
) and to_owned
on variables, but that's by no means a universal rule.
In summary, String
is the type of owned strings, and &str
is the type of borrowed string slices. String literals in Rust have type &'static str
.