不能使用条目 API 来使用引用作为函数内部的键来改变 HashMap
Cannot use the entry API to mutate a HashMap using a reference as the key inside of a function
我正在尝试获取可变 HashMap
引用中元素的句柄,其中的键是 &str
。
在下面的示例中,我尝试获取值 dict[key]
以便对其进行变异。我该怎么做?
我试过:
dict.entry(key)
:生命周期不匹配
dict.entry(&String::from(key))
: 借来的价值不够长
例如这个:
use std::collections::HashMap;
fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
let num = dict.entry(&String::from(key)).or_insert(0);
*num += 1;
return 42;
}
错误:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:4:27
|
3 | fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
| - let's call the lifetime of this reference `'1`
4 | let num = dict.entry(&String::from(key)).or_insert(0);
| ------------^^^^^^^^^^^^^^^^^- - temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| argument requires that borrow lasts for `'1`
Link key
参数的生命周期到 HashMap
:
中键的生命周期
use std::collections::HashMap;
fn do_thing<'a>(key: &'a str, dict: &mut HashMap<&'a str, u32>) -> u32 {
*dict.entry(key).or_insert(0) += 1;
42
}
dict.entry(key)
此版本的错误消息有助于理解问题:
use std::collections::HashMap;
fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
*dict.entry(key).or_insert(0) += 1;
42
}
error[E0623]: lifetime mismatch
--> src/lib.rs:4:17
|
3 | fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
| ---- ----
| |
| these two types are declared with different lifetimes...
4 | *dict.entry(key).or_insert(0) += 1;
| ^^^ ...but data from `key` flows into `dict` here
具体来说,entry
会将key
存储在HashMap
中,但key
引用的值可能会在[=47=之前失效] HashMap
会。如果发生这种情况,HashMap
将包含一个悬空引用,指向无效内存。这正是 Rust 的借用检查器所防止的。
另请参阅:
dict.entry(&String::from(key))
出于同样的原因,这在这里永远行不通。
另请参阅:
我正在尝试获取可变 HashMap
引用中元素的句柄,其中的键是 &str
。
在下面的示例中,我尝试获取值 dict[key]
以便对其进行变异。我该怎么做?
我试过:
dict.entry(key)
:生命周期不匹配dict.entry(&String::from(key))
: 借来的价值不够长
例如这个:
use std::collections::HashMap;
fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
let num = dict.entry(&String::from(key)).or_insert(0);
*num += 1;
return 42;
}
错误:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:4:27
|
3 | fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
| - let's call the lifetime of this reference `'1`
4 | let num = dict.entry(&String::from(key)).or_insert(0);
| ------------^^^^^^^^^^^^^^^^^- - temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| argument requires that borrow lasts for `'1`
Link key
参数的生命周期到 HashMap
:
use std::collections::HashMap;
fn do_thing<'a>(key: &'a str, dict: &mut HashMap<&'a str, u32>) -> u32 {
*dict.entry(key).or_insert(0) += 1;
42
}
dict.entry(key)
此版本的错误消息有助于理解问题:
use std::collections::HashMap;
fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
*dict.entry(key).or_insert(0) += 1;
42
}
error[E0623]: lifetime mismatch
--> src/lib.rs:4:17
|
3 | fn do_thing(key: &str, dict: &mut HashMap<&str, u32>) -> u32 {
| ---- ----
| |
| these two types are declared with different lifetimes...
4 | *dict.entry(key).or_insert(0) += 1;
| ^^^ ...but data from `key` flows into `dict` here
具体来说,entry
会将key
存储在HashMap
中,但key
引用的值可能会在[=47=之前失效] HashMap
会。如果发生这种情况,HashMap
将包含一个悬空引用,指向无效内存。这正是 Rust 的借用检查器所防止的。
另请参阅:
dict.entry(&String::from(key))
出于同样的原因,这在这里永远行不通。
另请参阅: