如何修复此功能:
fn func<'a>(x: &'a HashMap<&[u8], String>) -> Option<&'a String> {
let k = vec![1, 2, 3];
let k: &[u8] = &k;
x.get(&k)
}
字符串
我收到错误
error[E0515]: cannot return value referencing local variable `k`
--> src/lib.rs:6:5
|
5 | let k: &[u8] = &k;
| -- `k` is borrowed here
6 | x.get(&k)
| ^^^^^^^^^ returns a value referencing data owned by the current function
型
2条答案
按热度按时间xwmevbvl1#
字符串
注意删除的
&
。lf3rwulv2#
k
的简化版本,没有第二个语句:字符串
Playground