lean_string

Compact, clone-on-write string type for Rust with inline storage and a String-compatible API.

Library
Cargo
v0.7.0
11stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
54/100Fair
Development Activity92
Maintenance52
Community16
Maturity44
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture85
Code Quality84
Innovation78
Learning Curve80

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 str values
  • Niche optimization so Option<LeanString> is the same size as LeanString
  • An immutable LeanStr variant plus optional serde and no_std support

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 str values without heap allocation
  • Running string handling in no_std or 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search