lean_string
Compact, clone-on-write string type for Rust with inline storage and a String-compatible API.
Repository Health
Technical Analysis
lean_string provides LeanString, a memory-efficient replacement for Rust’s standard String. It occupies just two machine words, stores strings of up to 16 bytes inline on the stack (8 bytes on 32-bit targets), and moves to a reference-counted heap buffer only for larger values.
Cloning a LeanString is an O(1), allocation-free operation because the heap buffer is shared and copied only on mutation (clone-on-write). Construction from a &'static str is likewise zero-allocation. The crate keeps high API compatibility with the standard String, supports no_std environments, and includes an immutable LeanStr variant for read-only strings.
What You Get
LeanString, a two-word string that stores up to 16 bytes inline before spilling to the heap- Clone-on-write semantics: O(1) cloning with copy-on-mutation of the shared heap buffer
- Zero-allocation construction from
&'static strvalues - Niche optimization so
Option<LeanString>is the same size asLeanString - An immutable
LeanStrvariant plus optionalserdeandno_stdsupport
Common Use Cases
- Reducing memory footprint in programs that hold large numbers of short strings
- Cheaply cloning and sharing string data across threads or data structures
- Interning or storing
&'static strvalues without heap allocation - Running string handling in
no_stdor embedded contexts
Under The Hood
Architecture - The crate is built around a compact two-word representation defined in repr.rs and the repr/ module, which encodes whether a string is stored inline, as a shared reference-counted heap buffer, or as a static &'static str. lib.rs (~2,130 lines) exposes the LeanString and LeanStr public types and implements the String-compatible surface, delegating storage decisions to the representation layer. Trait implementations live in traits.rs and error types in errors.rs/errors/.
Tech Stack - Written in Rust using the 2024 edition, it depends on itoa and zmij for numeric formatting, castaway for zero-cost type specialization, and optional serde_core and arbitrary integrations. It is no_std-capable (default std feature) and uses loom under a cfg flag for concurrency model checking.
Code Quality - The project has a tests/ directory plus property-based testing via proptest, allocation profiling with dhat, benchmarks in bench/, and a maintained CHANGELOG.md. A pinned rust-toolchain.toml and rustfmt.toml enforce consistent builds and formatting, and development activity is high.
API Design - LeanString deliberately mirrors the standard String API (from, push, concatenation, comparisons) so it can be adopted as a near drop-in replacement, while documenting the trade-offs between it and the immutable LeanStr. The result is an ergonomic type that hides its representation complexity behind a familiar surface.