在模式匹配时,您可以使用 ref mut
来指定要获取所包含值的可变引用:
let mut score = Some(42);
if let Some(ref mut s) = score {
&mut s;
}
然而,内部值是不可变的:
error[E0596]: cannot borrow immutable local variable `s` as mutable
--> src/main.rs:4:14
|
4 | &mut s;
| ^
| |
| cannot reborrow mutably
| try removing `&mut` here
我尝试添加另一个
mut
,但是不合法:if let Some(mut ref mut s) = score {
&mut s;
}
error: the order of `mut` and `ref` is incorrect
--> src/main.rs:3:17
|
3 | if let Some(mut ref mut s) = score {
| ^^^^^^^ help: try switching the order: `ref mut`
error: expected identifier, found keyword `mut`
--> src/main.rs:3:25
|
3 | if let Some(mut ref mut s) = score {
| ^^^ expected identifier, found keyword
error: expected one of `)`, `,`, or `@`, found `s`
--> src/main.rs:3:29
|
3 | if let Some(mut ref mut s) = score {
| ^ expected one of `)`, `,`, or `@` here
&mut &mut T
吗? - Arjanas_mut
方法,而不仅仅是一般的语法。 - Shepmastermut var
然后借用可变的两次:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a87aaed059097e8a72817f09560d782e - Ömer Erdenmut
引用作为可变引用借用。 - Ömer Erden