Is it correct to say that these string types can be used:
&str, String, &String, and &mut String
but these types cannot be used?
str and &mut str
str is the unsized type, named string slice. It is simply a sequence of bytes somewhere in memory, with an additional constraint of them being the valid UTF-8 sequence. Since this type is unsized, i.e. length of this sequence is not encoded anywhere, it's impossible to use it without indirection.
&str is the shared reference to str. It is usually seen in two places: when referring to the strings encoded directly into binary (string literals), and as a result of dereferencing the &String.
&mut str is the unique reference to str. It is not generally useful, since it's hard to do something right with it - check this documentation (search &mut) for a total list of possible things.
String is an owned type, a kind of smart pointer, always containing str.
&String is a shared reference to String. In general, it is not very useful, since (almost) anything you can do with &String, you really do by (implicitly) dereferencing it to get &str.
&mut String is an unique reference to String. It can be used to do operations which may change string length, such as replacing substrings.
To answer your question directly:
-
&str,String,&mut Stringare generally used. -
&Stringand&mut strare almost always not the thing you want, but are possible. -
strcan't be used directly, but is conceptually the building block for anything else in this list.
You can't currently use str alone, however:
-
You can use it inside other types where the generic parameter can be
?Sized, for exampleRc<str>might make sense -
It might become usable when rust will allow unsized rvalues (RFC here)
&mut str makes sense for trasformations on ascii characters, for example str::make_ascii_lowercase and str::make_ascii_uppercase.