首页 > 解决方案 > 如何通过引用编辑“选项”中的值

问题描述

给定一些大结构的选项列表:

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

标签: rustreference

解决方案


调用:tester_method(tester[1].as_mut().unwrap());修复它。

.as_mut()


推荐阅读