如何有效地查找和插入HashMap?

编程入门 行业动态 更新时间:2024-10-28 21:20:01
本文介绍了如何有效地查找和插入HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

  • 查找 Vec 如果它不存在,为密钥创建一个空的 Vec ,并将其存储以备后用。
  • ,但仍然保留在变量中。

如何有效地执行此操作?自然我以为我可以用 match :

使用std :: collections :: HashMap的; //此代码不能编译。 let mut map = HashMap :: new(); let key =foo; let values:& Vec< isize> = match map.get(key){ Some(v)=> v, None => {让预设:Vec< isize> = Vec :: new(); map.insert(key,default); & amp; default } };

当我尝试它时,它给了我错误:

错误[E0502]:无法借用`map`作为可变因为它也被借用为不可变 - > src / main.rs:11:13 | 7 | let values:& Vec< isize> =匹配map.get(key){ | ---不变的借款发生在这里 ... 11 | map.insert(key,default); | ^^^可变借款发生在这里 ... 15 | } | - 不变的借款在这里结束

我最终做了这样的事情,但我不喜欢事实上它执行了两次查找( map.contains_key 和 map.get ):

//此代码编译。 let mut map = HashMap :: new(); let key =foo; if!map.contains_key(key){ let default:Vec< isize> = Vec :: new(); map.insert(key,default); } let values:& Vec< isize> = match map.get(key){ Some(v)=> v, None => {恐慌!(impossiburu!); } };

有没有一种安全的方法可以只用一个 match ?

解决方案

entry API 就是为此而设计的。在手动形式下,它可能看起来像

使用std :: collections :: hash_map :: Entry; 让值:& Vec< isize> = match map.entry(key){ Entry :: Occupied(o)=> o.into_mut(), Entry :: Vacant(v)=> v.insert(默认)};

或者可以使用简单的形式: < pre $ map.entry(key).or_insert_with(||默认)

如果默认即使未插入,也可以计算出来,但它也可以是:

map.entry(key).or_insert(默认)

I'd like to do the following:

  • Lookup a Vec for a certain key, and store it for later use.
  • If it doesn't exist, create an empty Vec for the key, but still keep it in the variable.

How to do this efficiently? Naturally I thought I could use match:

use std::collections::HashMap; // This code doesn't compile. let mut map = HashMap::new(); let key = "foo"; let values: &Vec<isize> = match map.get(key) { Some(v) => v, None => { let default: Vec<isize> = Vec::new(); map.insert(key, default); &default } };

When I tried it, it gave me errors like:

error[E0502]: cannot borrow `map` as mutable because it is also borrowed as immutable --> src/main.rs:11:13 | 7 | let values: &Vec<isize> = match map.get(key) { | --- immutable borrow occurs here ... 11 | map.insert(key, default); | ^^^ mutable borrow occurs here ... 15 | } | - immutable borrow ends here

I ended up with doing something like this, but I don't like the fact that it performs the lookup twice (map.contains_key and map.get):

// This code does compile. let mut map = HashMap::new(); let key = "foo"; if !map.contains_key(key) { let default: Vec<isize> = Vec::new(); map.insert(key, default); } let values: &Vec<isize> = match map.get(key) { Some(v) => v, None => { panic!("impossiburu!"); } };

Is there a safe way to do this with just one match?

解决方案

The entry API is designed for this. In manual form, it might look like

use std::collections::hash_map::Entry; let values: &Vec<isize> = match map.entry(key) { Entry::Occupied(o) => o.into_mut(), Entry::Vacant(v) => v.insert(default) };

Or one can use the briefer form:

map.entry(key).or_insert_with(|| default)

If default is OK/cheap to compute even when it isn't inserted, it can also just be:

map.entry(key).or_insert(default)

更多推荐

如何有效地查找和插入HashMap?

本文发布于:2023-10-27 22:27:11,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有效地   HashMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!