Rust 如何在选项数组中正确重新分配值?
正在尝试更新选项数组中的值:Rust 如何在选项数组中正确重新分配值?,rust,Rust,正在尝试更新选项数组中的值: static mut ILIST: [Option<u32>; 5] = [None, None, None, None, None]; fn main() { unsafe { ILIST[0] = Some(10); match &ILIST[0].as_mut() { None => println!("Is none"), Some(n)
static mut ILIST: [Option<u32>; 5] = [None, None, None, None, None];
fn main() {
unsafe {
ILIST[0] = Some(10);
match &ILIST[0].as_mut() {
None => println!("Is none"),
Some(n) => {
*n = 5;
},
}
match ILIST[0] {
None => println!("Is none"),
Some(n) => {
assert_eq!(n, 5);
},
}
}
}
将指定的代码更新为以下内容:
Some(n) => {
**n = 5;
},
导致另一个编译器错误:
error[E0308]: mismatched types
--> src/main.rs:17:10
|
17 | *n = 5;
| ^ expected `&mut u32`, found integer
|
help: consider dereferencing here to assign to the mutable borrowed piece of memory
|
17 | **n = 5;
| ^^^
error[E0594]: cannot assign to `**n` which is behind a `&` reference
--> src/main.rs:17:5
|
17 | **n = 5;
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
有没有发现这里出了什么问题?谢谢 您不需要参考:
static mutilist:[Option;5]=[None,None,None,None,None];
fn main(){
不安全{
ILIST[0]=一些(10);
//不是裁判
匹配ILIST[0]。作为_mut(){
无=>println!(“无”),
一些(n)=>*n=5,
}
匹配ILIST[0]{
无=>println!(“无”),
一些(n)=>断言(n,5),
}
}
}
删除第一个匹配中的和:匹配ILIST[0]。as_mut()
。