Crates.io | simple-symbol |
lib.rs | simple-symbol |
version | 3.1.0 |
source | src |
created_at | 2018-07-17 14:18:24.79572 |
updated_at | 2021-01-03 16:06:49.4995 |
description | Convenient, basic String interning. |
homepage | |
repository | https://github.com/nwtnni/simple-symbol |
max_upload_size | |
id | 74707 |
size | 9,597 |
There are already a lot of string interning libraries out there, so this one is mostly just for my personal use case: writing a compiler without passing around a struct everywhere.
use simple_symbol::{intern, resolve};
pub fn main() {
let a = intern("A");
let b = intern("A");
assert_eq!(a, a);
let c = intern("B");
assert_ne!(a, c);
assert_ne!(b, c);
// Prints "A"
println!("{}", a);
let str_a = resolve(a);
assert_eq!(str_a, "A");
}
Symbols are compared via usize
indices, and automatically
query the global INTERNER
struct when printing or converting.
Leaks all interned Strings for the duration of the program. Unsuitable for long-running programs.
3.1.0
once_cell
from lazy_static
.3.0.0
intern
function to take the more common S: AsRef<str>
instead of S: Into<Cow<'a, str>>
.intern_static
function to avoid leaking already 'static
data.2.0.0
lazy_static
to support multi-threaded programs.1.0.0
PartialOrd
and Ord
for Symbol
for easier use as keys in crates like petgraph
.0.1.0