Enum std::collections::btree_map::Entry1.0.0[][src]

pub enum Entry<'a, K, V> where
    K: 'a,
    V: 'a, 
{ Vacant(VacantEntry<'a, K, V>), Occupied(OccupiedEntry<'a, K, V>), }
Expand description

map 中单个条目的视图,该条目可能是空的或被已占用。

enum 是根据 BTreeMap 上的 entry 方法构造的。

Variants

Vacant(VacantEntry<'a, K, V>)

一个空的条目。

Occupied(OccupiedEntry<'a, K, V>)

一个被占用的条目。

Implementations

如果为空,则通过插入默认值来确保该值在条目中,并返回对条目中值的可变引用。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, usize> = BTreeMap::new();
map.entry("poneyland").or_insert(12);

assert_eq!(map["poneyland"], 12);
Run

如果为空,则通过插入默认函数的结果来确保该值在条目中,并返回对条目中值的可变引用。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, String> = BTreeMap::new();
let s = "hoho".to_string();

map.entry("poneyland").or_insert_with(|| s);

assert_eq!(map["poneyland"], "hoho".to_string());
Run

如果为空,则通过插入默认函数的结果,确保值在条目中。 通过为 .entry(key) 方法调用期间移动的键提供默认函数引用,此方法可以生成用于插入的键派生值。

提供了对已移动键的引用,因此不需要克隆或复制键,这与 .or_insert_with(|| ... ) 不同。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, usize> = BTreeMap::new();

map.entry("poneyland").or_insert_with_key(|key| key.chars().count());

assert_eq!(map["poneyland"], 9);
Run

返回此条目的键的引用。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, usize> = BTreeMap::new();
assert_eq!(map.entry("poneyland").key(), &"poneyland");
Run

在任何潜在的插入 map 之前,提供对占用条目的就地可变访问。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, usize> = BTreeMap::new();

map.entry("poneyland")
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map["poneyland"], 42);

map.entry("poneyland")
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map["poneyland"], 43);
Run

如果为空,则通过插入默认值来确保值在条目中,并向条目中的值返回变量引用。

Examples

use std::collections::BTreeMap;

let mut map: BTreeMap<&str, Option<usize>> = BTreeMap::new();
map.entry("poneyland").or_default();

assert_eq!(map["poneyland"], None);
Run

Trait Implementations

使用给定的格式化程序格式化该值。 Read more

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。