# trie-rs KV capable prefix trie library based on LOUDS. This is a fork of https://laysakura.github.io/trie-rs/trie_rs. ## Quickstart To use trie-rs, add the following to your `Cargo.toml` file: ```toml [dependencies] trie-rs = "0.1" # NOTE: Replace to latest minor version. ``` ### Usage Overview ```rust use std::str; use trie_rs::TrieBuilder; let mut builder = TrieBuilder::new(); // Inferred `TrieBuilder` automatically builder.push("すし", 1); builder.push("すしや", 2); builder.push("すしだね", 3); builder.push("すしづめ", 4); builder.push("すしめし", 5); builder.push("すしをにぎる", 6); builder.push("すし", 7); // Word `push`ed twice is just ignored. builder.push("🍣", 8); let mut trie = builder.build(); // exact_match(): Find a word exactly match to query. assert_eq!(trie.exact_match("すし"), true); assert_eq!(trie.exact_match("🍣"), true); assert_eq!(trie.exact_match("🍜"), false); // predictive_search(): Find words which include `query` as their prefix. let results_in_u8s: Vec> = trie.predictive_search("すし"); let results_in_str: Vec<&str> = results_in_u8s .iter() .map(|u8s| str::from_utf8(u8s).unwrap()) .collect(); assert_eq!( results_in_str, vec![ "すし", "すしだね", "すしづめ", "すしめし", "すしや", "すしをにぎる" ] // Sorted by `Vec`'s order ); // common_prefix_search(): Find words which is included in `query`'s prefix. let results_in_u8s: Vec> = trie.common_prefix_search("すしや"); let results_in_str: Vec<&str> = results_in_u8s .iter() .map(|u8s| str::from_utf8(u8s).unwrap()) .collect(); assert_eq!( results_in_str, vec![ "すし", "すしや", ] // Sorted by `Vec`'s order ); // common_prefix_search_with_value(): Find words which is included in `query`'s prefix and return their values. let results_in_u8s: Vec<(Vec, u8)> = trie.common_prefix_search_with_values("すしや"); let results_in_str: Vec<(&str, u8)> = results_in_u8s .iter() .map(|(u8s, v)| (str::from_utf8(u8s).unwrap(), *v)) .collect(); assert_eq!( results_in_str, vec![ ("すし", 1), ("すしや", 2), ] // Sorted by `Vec`'s order ); // get_value(): Get value of a word. assert_eq!(trie.get("すし"), Some(&1)); // set value in a built trie. trie.set("すし", 9); assert_eq!(trie.get("すし"), Some(&9)); ``` ### Using with Various Data Types `TrieBuilder` is implemented using generic type like following: ``` impl TrieBuilder