如何正确使用HashMap ::条目? [英] How to properly use HashMap::entry?
问题描述
这段代码:
使用std :: collections :: HashMap;
使用std :: collections :: hash_map :: Entry :: {占用,空置};
fn main(){
let mut vars = HashMap ::< i32,f64> :: new();
let key = 10;
let val = match vars.entry(key){
Vacant(entry)=> entry.set(0.0),
已占用(entry)=> entry.into_mut(),
};
* val + = 3.4;
println!({},val);
$ b $ / code>
在围栏中给出这样的错误:
:8:29:8:37错误:type
std :: collections :: hash :: map :: VacantEntry <'_ ,i32,f64>
没有实现名为set
:8 Vacant(entry)=> entry.set 0.0),
^ ~~~~~~~
错误:由于前面的错误而中止
围栏:应用程序以错误代码101终止
这是因为 VacantEntry
没有实现任何名为设置
,但有一个名为插入
的方法。 API参考,围栏。
This snippet of code:
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
fn main() {
let mut vars = HashMap::<i32, f64>::new();
let key = 10;
let val = match vars.entry(key) {
Vacant(entry) => entry.set(0.0),
Occupied(entry) => entry.into_mut(),
};
*val += 3.4;
println!("{}", val);
}
give error like this in playpen:
:8:29: 8:37 error: type
std::collections::hash::map::VacantEntry<'_, i32, f64>
does not implement any method in scope namedset
:8 Vacant(entry) => entry.set(0.0), ^~~~~~~~ error: aborting due to previous error playpen: application terminated with error code 101
It's because VacantEntry
doesn't implement any method named set
, but there is method named insert
. api reference, playpen.
这篇关于如何正确使用HashMap ::条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!