rust - 如何通过引用编辑“选项”中的值
问题描述
给定一些大结构的选项列表:
struct big_struct {
a:i32 // Imagine this is some big datatype
}
let mut tester:Vec<Option<(big_struct,big_struct)>> = vec![
None,
Some((big_struct{a:1},big_struct{a:2})),
None,
];
我正在遍历这个列表,通过另一个检查我知道哪些元素有Some
,在这些元素上我想调用一个函数来编辑它们的值
示例函数:
fn tester_method(pos:&mut (big_struct,big_struct)) {
pos.0 = big_struct {a:8};
pos.1 = big_struct {a:9};
}
不起作用的函数调用:tester_method(&mut tester[1].unwrap());
我怎样才能做到这一点?
在函数调用中,我收到错误:
cannot move out of index of `std::vec::Vec<std::option::Option<(big_struct, big_struct)>>`
move occurs because value has type `std::option::Option<(big_struct, big_struct)>`, which does not implement the `Copy` traitrustc(E0507)
main.rs(132, 24): consider borrowing the `Option`'s content
解决方案
调用:tester_method(tester[1].as_mut().unwrap());
修复它。
推荐阅读
- xamarin - 我想知道如何通过用户单击事件将 xamarin 表单中的开关按钮状态保存为 ON 或 OFF
- shell - 使用 shell 脚本从字符串 YYYYQ1 中提取季度日期
- c++ - 如何使用 C++ 在 LUA 中编写具有对话和菜单的交互式 NPC 脚本?
- google-apps-script - 谷歌表格 - 使用谷歌脚本将整列中的数据从 KB 转换为 GB
- android - Flutter 中的 HTTP 请求
- tensorflow - 如何分析在 tf-serving 上运行的 tensorflow 模型?
- docker-compose - 将 Authorization & Origin 标头添加到 traefik 反向代理
- python - 使用 opencv 自动校正歪斜
- git - git push期间索引包异常退出
- php - 如何使用switch设置多个条件?