1 个稳定版本
1.0.0 | 2023年3月14日 |
---|
#2 在 #cheaply
20KB
248 行代码(不包括注释)
gregtatum_symbol_table
一种快速高效的符号表,使字符串的廉价操作变得简单。
存储一个独特的字符串列表,使得可以通过稳定的索引来操作字符串,这些索引存储在 Symbol
类型中。这使得字符串的便宜比较和引用的简单存储变得容易。字符串作为 Symbol
访问,该 Symbol
具有下面的函数:
use gregtatum_symbol_table::SymbolTable;
let symbol_table = SymbolTable::new();
// Insert strings into the SymbolTable.
let hello_symbol = symbol_table.get("hello");
let world_symbol = symbol_table.get("world");
// Strings can easily be accessed.
assert_eq!(hello_symbol.str(), String::from("hello"));
String 可以通过各种方便的特性进行访问
let hello_string: String = hello_symbol.into();
assert_eq!(hello_string, "hello");
let hello_string: &str = hello_symbol.as_ref();
assert_eq!(hello_string, "hello");
let hello_string: String = format!("{}", hello_symbol);
assert_eq!(hello_string, "hello");
// String equality works across Symbols and strings.
assert_eq!(hello, "hello");
assert_eq!(world, "world");
有各种方便的方法来获取符号并与之交互。
// The symbol can be looked up via a HashMap, and string comparison will cheaply
// compare the indexes, and avoid full string comparison.
assert_eq!(symbol_table.get("hello"), hello_symbol);
let hello_world = symbol_table.get("hello world");
let hello_slice = hello_world.slice(0..5).unwrap();
// Slices can easily be created, but string comparison is now a full comparison.
assert_eq!(hello_slice, hello_symbol);
// But slices can be turned back into full Symbols for cheap comparisons.
assert_eq!(hello_slice.deslice(), hello_symbol);
依赖项
~230KB