struct - 如何在指向同一结构中的数据的结构中存储可变引用?
问题描述
struct ChunkUnpacketizer<'a> {
id_2_chunk_info: HashMap<u32, ChunkInfo>,
current_chunk_info: &'a mut ChunkInfo,
}
impl<'a> ChunkUnpacketizer<'a> {
fn read_basic_header(&mut self) {
let id = 32 as u32;
match self.id_2_chunk_info.get_mut(&id) {
Some(chunk_info) => {
self.current_chunk_info = chunk_info;
}
None => {
self.id_2_chunk_info.insert(id, ChunkInfo::new());
}
}
}
}
我想要的是self.current_chunk_info
与 got mut 值具有相同的生命周期HashMap
,但我得到了以下错误:
cannot infer an appropriate lifetime for autoref due to conflicting requirements
first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 175:5...
...so that reference does not outlive borrowed content
but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 74:6...
...so that reference does not outlive borrowed content
怎么修?
解决方案
推荐阅读
- python - 通过熊猫曲线识别“相似”数据集
- html - 将 TD Align="center" 转换为 CSS
- c++ - 在 C++ 中解析整个(开始到结束)字符串,包括所有空格
- .net - 如何设置固定到任务栏的应用程序的 AppUserModelId?
- python - 如何在 Odoo 中向 One2many 字段添加约束?
- r - 如何根据 shinymanager 凭据禁用闪亮功能
- c - 将所有列表值更改为最后一个值
- ruby - lita bot 对触发器没有响应
- java - 如何从java中的HttpServletRequest中获取调用哪个端点操作?
- c++ - 关于 std::condition_variables 的两个问题