Thanks to visit codestin.com
Credit goes to lib.rs

2 releases

Uses new Rust 2024

0.1.1 Aug 26, 2025
0.1.0 Aug 26, 2025

#312 in Concurrency

Codestin Search App Codestin Search App Codestin Search App Codestin Search App

67 downloads per month

MIT license

50KB
1K SLoC

str_cache

A lockfree string interning library for Rust.

What is this?

String interning lets you store each unique string only once in memory, then reference it through lightweight handles. This is useful for programs that would otherwise end up with lots of duplicate String allocations or need to leak strings to &'static str.

When to use it

  • You're parsing files with repeated strings (JSON keys, XML tags, etc.)
  • You have long-lived data structures with duplicate strings
  • You want the benefits of &'static str references
  • Your program creates many temporary strings that could be deduplicated

Basic usage

use str_cache::{intern, get};

// Intern strings - duplicates share the same memory
let hello1 = intern("hello");
let hello2 = intern("hello");  // Same pointer as hello1

// Get string content back
assert_eq!(&*hello1, "hello");

// Look up existing strings
assert_eq!(get("hello"), Some(hello1));
assert_eq!(get("missing"), None);

Advanced usage

For cases where you need multiple independent caches:

use str_cache::StrCache;

let cache = StrCache::new();
let handle = cache.intern("some string");
let content: &str = &*handle;

Features

  • Memory efficient: Each unique string stored only once
  • Lock-free: No mutexes or blocking - scales well under contention
  • Thread safe: Safe to use from multiple threads
  • Zero-copy: Handles dereference to &str without copying
  • Fast lookups: O(1) average case for both intern and lookup
  • Clone-free: No need to clone strings to share them
  • Fast equality: Handles compare by pointer equality for fast comparison
  • Miri tested: Runs under Miri for memory safety checks (with provenance disabled)

The handles are small, cheap to copy, and implement all the standard traits (Eq, Ord, Hash, etc.) so you can use them as map keys, in collections, or anywhere you'd use a string.

License

MIT

Dependencies

~2MB
~30K SLoC