10 hours ago
- Rust strings involve two main types: `str` (string slice) and `String` (owned string), both guaranteed to be UTF-8 encoded.
- `str` is a dynamically sized type (DST) behind a wide pointer, while `String` wraps a `Vec<u8>` and supports heap allocation and mutation.
- Deref coercion and dot operator semantics allow methods like `chars()` on `String` by automatically dereferencing to `str`.
- The `len()` method returns byte count, not character count, due to UTF-8 variable-length encoding.
- Operator overloading (e.g., `+` for concatenation) and trait implementations like `PartialEq` enable ergonomic string operations.
- In-place methods like `make_ascii_uppercase()` work on `&mut str` but cannot handle non-ASCII characters that change byte length.
- Other string types include `OsString`/`OsStr`, `CString`/`CStr`, and experimental `ByteString`/`Char` for specific use cases.