| Crates.io | lean_string |
| lib.rs | lean_string |
| version | 0.5.1 |
| created_at | 2024-12-08 12:45:34.789232+00 |
| updated_at | 2025-08-24 07:37:36.366085+00 |
| description | Compact, clone-on-write string. |
| homepage | |
| repository | https://github.com/ryota2357/lean_string |
| max_upload_size | |
| id | 1476262 |
| size | 206,232 |
Compact, clone-on-write string.
LeanString has the following properties:
size_of::<LeanString>() == size_of::<[usize; 2]>() (2 words).
usize smaller than String.LeanString uses a reference-counted heap buffer (like Arc).LeanString is cloned, the heap buffer is shared.LeanString is mutated, the heap buffer is copied if it is shared.O(1), zero allocation construction from &'static str.Option<LeanString>.
size_of::<Option<LeanString>>() == size_of::<LeanString>()String.no_std environment.use lean_string::LeanString;
// This is a zero-allocation operation, stored inlined.
let small = LeanString::from("Hello");
// More than 16 bytes, stored on the heap (64-bit architecture).
let large = LeanString::from("This is a not long but can't store inlined");
// Clone is O(1), heap buffer is shared.
let mut cloned = large.clone();
// Mutating a shared string will copy the heap buffer. (CoW)
cloned.push('!');
assert_eq!(cloned, "This is a not long but can't store inlined!");
assert_eq!(large + "!", cloned);
| Name | Size | Inline | &'static str |
Notes |
|---|---|---|---|---|
String |
24 bytes | No | No | prelude |
Cow<'static, str> |
24 bytes | No | Yes | std (alloc) |
CompactString |
24 bytes | 24 bytes | Yes | Nich optimized for Option<_> |
EcoString |
16 bytes | 15 bytes | No | Clone-on-Write, Nich optimized for Option<_> |
LeanString (This crate) |
16 bytes | 16 bytes | Yes | Clone-on-Write, Nich optimized for Option<_> |
| Name | Size | Inline | &'static str |
Notes |
|---|---|---|---|---|
String |
12 bytes | No | No | prelude |
Cow<'static, str> |
12 bytes | No | Yes | std (alloc) |
CompactString |
12 bytes | 12 bytes | Yes | Nich optimized for Option<_> |
EcoString |
8 bytes | 7 bytes | No | Clone-on-Write, Nich optimized for Option<_> |
LeanString (This crate) |
8 bytes | 8 bytes | Yes | Clone-on-Write, Nich optimized for Option<_> |
&'static str: Zero-allocation and O(1) construction from &'static str.Other string types may have different properties and use cases.
For more comparison and information, please see Rust String Benchmarks.
The idea and implementation of LeanString is inspired by the following projects:
I would like to thank the authors of these projects for their great work.
This crate is licensed under the MIT license.