CompactStr
is a compact immutable string type that stores itself on the stack, if possible, and seamlessly
interacts with String
s and &str
s.
Memory Layout
Normally strings are stored on the heap, since they're dynamically sized. In Rust a String
consists of
three things:
- A
usize
denoting the length of the string - A pointer to a location on the heap where the string is stored
- A
usize
denoting the capacity of the string
On 64-bit architectures this results in 24 bytes being stored on the stack, 12 bytes for 32-bit architectures. For small strings, e.g. < 23 characters