Struct std::collections::hash_map::OccupiedEntry 1.0.0[−][src]
pub struct OccupiedEntry<'a, K: 'a, V: 'a> { /* fields omitted */ }
Expand description
HashMap
中已占用条目的视图。
它是 Entry
枚举的一部分。
Implementations
从 map 获取键和值的所有权。
Examples
use std::collections::HashMap;
use std::collections::hash_map::Entry;
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(12);
if let Entry::Occupied(o) = map.entry("poneyland") {
// 我们从 map 中删除了这个条目。
o.remove_entry();
}
assert_eq!(map.contains_key("poneyland"), false);
Run获取条目中的值的可变引用。
如果需要对 OccupiedEntry
的引用,而这可能会使 Entry
值的破坏失效,请参见 into_mut
。
Examples
use std::collections::HashMap;
use std::collections::hash_map::Entry;
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(12);
assert_eq!(map["poneyland"], 12);
if let Entry::Occupied(mut o) = map.entry("poneyland") {
*o.get_mut() += 10;
assert_eq!(*o.get(), 22);
// 我们可以多次使用同一个 Entry。
*o.get_mut() += 2;
}
assert_eq!(map["poneyland"], 24);
Run将 OccupiedEntry
转换为条目中带有生命周期绑定到 map 本身的值的变量引用。
如果需要多次引用 OccupiedEntry
,请参见 get_mut
。
Examples
use std::collections::HashMap;
use std::collections::hash_map::Entry;
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(12);
assert_eq!(map["poneyland"], 12);
if let Entry::Occupied(o) = map.entry("poneyland") {
*o.into_mut() += 10;
}
assert_eq!(map["poneyland"], 22);
Run设置条目的值,并返回条目的旧值。
Examples
use std::collections::HashMap;
use std::collections::hash_map::Entry;
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(12);
if let Entry::Occupied(mut o) = map.entry("poneyland") {
assert_eq!(o.insert(15), 12);
}
assert_eq!(map["poneyland"], 15);
Run从条目中取出值,然后将其返回。
Examples
use std::collections::HashMap;
use std::collections::hash_map::Entry;
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(12);
if let Entry::Occupied(o) = map.entry("poneyland") {
assert_eq!(o.remove(), 12);
}
assert_eq!(map.contains_key("poneyland"), false);
Run替换条目,返回旧的键和值。 哈希 map 中的新键将是用于创建此条目的键。
Examples
#![feature(map_entry_replace)]
use std::collections::hash_map::{Entry, HashMap};
use std::rc::Rc;
let mut map: HashMap<Rc<String>, u32> = HashMap::new();
map.insert(Rc::new("Stringthing".to_string()), 15);
let my_key = Rc::new("Stringthing".to_string());
if let Entry::Occupied(entry) = map.entry(my_key) {
// 同时用我们其他键的句柄代替键。
let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
}
Run用用于创建此条目的键替换哈希 map 中的键。
Examples
#![feature(map_entry_replace)]
use std::collections::hash_map::{Entry, HashMap};
use std::rc::Rc;
let mut map: HashMap<Rc<String>, u32> = HashMap::new();
let known_strings: Vec<Rc<String>> = Vec::new();
// 初始化已知的字符串,运行程序等
reclaim_memory(&mut map, &known_strings);
fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
for s in known_strings {
if let Entry::Occupied(entry) = map.entry(Rc::clone(s)) {
// 将条目的键替换为我们在 `known_strings` 中的版本。
entry.replace_key();
}
}
}
Run