A fast and efficient symbol table for making it easy to work cheaply with strings.
Stores a unique list of strings, so that strings can be operated upon via stable
indexes, which are stored in the Symbol
type. This makes for cheap comparisons
and easy storage of references to strings. The strings are accessed as Symbol
s
that have a fn str() -> &str
.
```rs use gregtatumsymboltable::SymbolTable;
let symbol_table = SymbolTable::new();
// Insert strings into the SymbolTable. let hellosymbol = symboltable.get("hello"); let worldsymbol = symboltable.get("world");
// Strings can easily be accessed. asserteq!(hellosymbol.str(), String::from("hello")); ```
String can be accessed via various convenient traits:
```rs let hellostring: String = hellosymbol.into(); asserteq!(hellostring, "hello");
let hellostring: &str = hellosymbol.asref(); asserteq!(hello_string, "hello");
let hellostring: String = format!("{}", hellosymbol); asserteq!(hellostring, "hello");
// String equality works across Symbols and strings. asserteq!(hello, "hello"); asserteq!(world, "world"); ```
There are various convenient ways to get a symbol back, and work with them.
```rs // The symbol can be looked up via a HashMap, and string comparison will cheaply // compare the indexes, and avoid full string comparison. asserteq!(symboltable.get("hello"), hello_symbol);
let helloworld = symboltable.get("hello world"); let helloslice = helloworld.slice(0..5).unwrap();
// Slices can easily be created, but string comparison is now a full comparison. asserteq!(helloslice, hello_symbol);
// But slices can be turned back into full Symbols for cheap comparisons. asserteq!(helloslice.deslice(), hello_symbol); ```