Crate interner

Source
Expand description

String interner

Interning strings gives us two benefits.

  1. Less memory usage, since we don’t need to over-allocate memory. If we intern “abcd” 1000 times, we will only need 4 bytes. Compared with using 1000 Strings, which would cause 1000 allocations

  2. Faster comparison. Instead of comparing string slices, which takes a lot of time, comparing symbols (which are represented as an usize) is much more faster. Since all calls to Symbol::new with the same string will resolve to the same Symbol, we can just compare the symbols.

§Example

use interner::Symbol;

let str1 = Symbol::new("abcdef");
let same = Symbol::new("abcdef");
assert_eq!(str1, same);

str1.borrow(|s| assert_eq!(s, "abcdef"));

Structs§

Symbol
Identifies an interned string.