为什么Rust会使用ref
关键字?可以
match value.try_thing() {
&Some(ref e) => do_stuff(e),
// ...
}
不能用相同的方式表达>
match value.try_thing() {
&Some(e) => do_stuff(&e),
// ...
}
为什么Rust没有ref关键字?可以匹配value.try_thing(){&Some(ref e)=> do_stuff(e),// ...}不能由match value.try_thing(){&Some(e)=> ...
不,您建议的语法无法避免。您的语法不允许引用,否则将允许移动。在此示例中,inner
是val
中整数的副本,更改它对val
没有影响:
fn main() {
let mut val = Some(42);
if let &mut Some(mut inner) = &mut val {
inner += 1;
}
println!("{:?}", val); // Some(42)
}