当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust BTreeMap.last_entry用法及代码示例


本文简要介绍rust语言中 alloc::collections::btree_map::BTreeMap.last_entry 的用法。

用法

pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> where    K: Ord,

返回Map中的最后一个条目以进行就地操作。此条目的键是映射中的最大键。

例子

#![feature(map_first_last)]
use std::collections::BTreeMap;

let mut map = BTreeMap::new();
map.insert(1, "a");
map.insert(2, "b");
if let Some(mut entry) = map.last_entry() {
    if *entry.key() > 0 {
        entry.insert("last");
    }
}
assert_eq!(*map.get(&1).unwrap(), "a");
assert_eq!(*map.get(&2).unwrap(), "last");

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 alloc::collections::btree_map::BTreeMap.last_entry。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。