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

Method get

code/chapter05/hashmap.rs:96–121  ·  view source on GitHub ↗
(&self, key: usize)

Source from the content-addressed store, hash-verified

94 }
95
96 fn get(&self, key: usize) -> Option<&T> {
97 if 0 == key { panic!("Error: key must > 0"); }
98
99 // 计算数据位置
100 let pos = self.hash(key);
101 let mut data: Option<&T> = None;
102 let mut stop = false;
103 let mut found = false;
104 let mut curr = pos;
105
106 // 循环查找数据
107 while 0 != self.slot[curr] && !found && !stop {
108 if key == self.slot[curr] {
109 found = true;
110 data = self.data.get(curr);
111 } else {
112 // 再哈希回到最初位置,说明找了一圈还没有
113 curr = self.rehash(curr);
114 if curr == pos {
115 stop = true;
116 }
117 }
118 }
119
120 data
121 }
122
123 fn contains(&self, key: usize) -> bool {
124 if 0 == key { panic!("Error: key must > 0"); }

Callers

nothing calls this directly

Calls 2

hashMethod · 0.45
rehashMethod · 0.45

Tested by

no test coverage detected