【发布时间】:2021-05-17 07:41:43
【问题描述】:
我的代码如下所示:
use std::collections::HashMap;
fn main() {
let x = get_hash_map();
println!("{:?}", x);
}
fn get_hash_map() -> Option<&'static Vec<i32>> {
let mut hm = HashMap::new();
let mut vec = Vec::new();
vec.push(1);
hm.insert("1".to_string(), vec);
return hm.get("1");
}
但是我收到了这个错误:
error[E0515]: cannot return value referencing local variable `hm`
--> src/main.rs:13:12
|
13 | return hm.get("1");
| --^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `hm` is borrowed here
任何人都可以建议最低限度地解决此问题的替代方法吗?谢谢!
【问题讨论】:
-
你的 hashmap 是本地的。它在函数结束时不再存在。你想做什么?
标签: rust rust-cargo