【发布时间】:2020-07-15 06:04:49
【问题描述】:
考虑以下几点:
use std::collections::HashMap;
use std::vec::Vec;
use crate::core::process_call_backs::SimpleCallback;
pub fn make_adventure_list(adventure_list: Vec<SimpleCallback>) -> HashMap<&i32, SimpleCallback> {
let mut adventures = HashMap::new();
let mut count = 1;
for adventure in adventure_list {
adventures.insert(count, adventure);
count = count + 1;
}
adventures;
}
我得到错误:
error[E0106]: missing lifetime specifier
--> core/src/core/create_adventures.rs:5:76
|
5 | pub fn make_adventure_list(adventure_list: Vec<SimpleCallback>) -> HashMap<&i32, SimpleCallback> {
| ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
|
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
我了解此错误的含义,但不确定如何实施修复。我需要使adventure_list 可变吗?
【问题讨论】:
-
快速解决方案是从
&i32中删除&,您几乎可以肯定需要一个32 位整数作为密钥,而不是对32 位整数的引用。 -
考虑重读本书相关章节:Chapter 4, What is Ownership?
-
这能回答你的问题吗? How do I fix a missing lifetime specifier?
标签: rust