Expand description
String interner
Interning strings gives us two benefits.
-
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
-
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::newwith 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.