A Survey of Rust Strings
Why does Rust have so many string types? How are they different, when should we use each one, and do they have analogues in other programming languages?
This post will try to address these questions by traversing the following families of types, roughly in order from least abstract to most abstract.
Native Rust
These types are native in the sense that almost all Rust code that interacts solely with other Rust code (vs. with other programming languages or the operating system) will stick to these types.
char
What is a character, and how do we represent it in a computer? Both questions are (perhaps surprisingly) quite nuanced, and carry a lot of history.
As an American English-speaking programmer, I used to
think of a character as a 7-bit ASCII-encoded value,
which C stores in its 8-bit char type.
Today, while the world has mostly settled on Unicode, there is still
Unlike C, where a char is (at least, but typically exactly) 1 byte
u8, [u8], &[u8], and Vec<u8>
- [\[u8\]](https://doc.rust-lang.org/std/primitive.slice.html), &\[u8\], and [Vec\<u8\>](https://doc.rust-lang.org/std/vec/struct.Vec.html)
str, &str, and String
- [str](https://doc.rust-lang.org/std/primitive.str.html), &str, and [String](https://doc.rust-lang.org/std/string/struct.String.html)
&'static str
- &'static str
Foreign Function Interface (FFI)
CStr and CString
- [std::ffi::CString](https://doc.rust-lang.org/std/ffi/struct.CString.html) and [std::ffi::CStr](https://doc.rust-lang.org/std/ffi/struct.CStr.html)
Path and PathBuf
- [std::path::PathBuf](https://doc.rust-lang.org/std/path/struct.PathBuf.html) and [std::path::Path](https://doc.rust-lang.org/std/path/struct.Path.html)
OsStr and OsString
- [std::ffi::OsString](https://doc.rust-lang.org/std/ffi/struct.OsString.html) and [std::ffi::OsStr](https://doc.rust-lang.org/std/ffi/struct.OsStr.html)
Optimization
Cow<str>
- [Cow\<str\>](https://doc.rust-lang.org/std/borrow/enum.Cow.html)
Small String Optimization (SSO)
- [Small String Optimization (SSO)](https://github.com/rosetta-rs/string-rosetta-rs)