MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / insert

Method insert

code/chapter09/trie.rs:20–29  ·  view source on GitHub ↗

单词插入

(&mut self, word: &str)

Source from the content-addressed store, hash-verified

18
19 // 单词插入
20 fn insert(&mut self, word: &str) {
21 let mut node = &mut self.root;
22 // 逐个字符插入
23 for c in word.as_bytes() {
24 let index = (c - b'a') as usize;
25 let next = &mut node.children[index];
26 node = next.get_or_insert_with(Box::<Node>::default);
27 }
28 node.end = true;
29 }
30
31 fn search(&self, word: &str) -> bool {
32 self.word_node(word).map_or(false, |n| n.end)

Callers 1

mainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected