30 releases (3 stable)

1.2.0 Jul 29, 2023
1.0.0 Jun 20, 2023
0.13.0 Mar 21, 2023
0.10.0 Dec 17, 2022
0.9.0 Nov 16, 2022

#293 in Rust patterns

Download history 26/week @ 2023-08-13 7/week @ 2023-08-20 68/week @ 2023-08-27 43/week @ 2023-09-03 21/week @ 2023-09-10 81/week @ 2023-09-17 92/week @ 2023-09-24 61/week @ 2023-10-01 46/week @ 2023-10-08 23/week @ 2023-10-15 28/week @ 2023-10-22 61/week @ 2023-10-29 26/week @ 2023-11-05 34/week @ 2023-11-12 24/week @ 2023-11-19 111/week @ 2023-11-26

204 downloads per month
Used in 5 crates


2.5K SLoC

This library defines strings with copy-on-write semantics.


Is a String that can be initialized to a static string or be build dynamically. Its contents can then be immutably shared and reference counted. When mutation is required the string will be copied first.

This resembles a improved Arc<Cow<'static, str>> with some differences:

  • No double dereference going from Arc to String to the actual data.
  • There are no Weak references.
  • CowStr can be interior-mutable extended (append at the end).
  • Improved AllocationStrategy

CowStr implements many of the std::String methods. Missing methods will be added as required, PR's are welcome.


Refers to an immutable slice inside of a CowStr. This somewhat resembles the String/&str relationship while SubStr keeping a strong reference to its orgin CowStr and thus don't need lifetimes.


  • serde wrapping the default string (de)serialization.
  • nightly enable some nightly optimizations and extensions

Implementation Notes

CowStr is made for sharing immutable strings, a short single CowStr will occupy slightly more memory than a std String. But as soon a CowStr is cloned or SubStr's are used it pays back. Especially since SubStr don't need lifetimes as they use reference counting memory management becomes significantly easier.

Because rust has yet incomplete support for DST's CowStr needs some unsafe code. This is liberally chosen, when possible contracts are enforced by debug_assert's.


CowStr commes with an extensive test suite. 'cargo-mutants' is used to detect missing tests. Releases must pass testing under 'miri'.