Clippy警告这样的代码:
fn func<T>(data: &Option<T>) {
if let &Some(ref value) = data {}
}
warning: you don't need to add `&` to all patterns
--> src/main.rs:2:5
|
2 | if let &Some(ref value) = data {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(match_ref_pats)] on by default
= help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.210/index.html#match_ref_pats
help: instead of prefixing all patterns with `&`, you can dereference the expression
|
2 | if let Some(ref value) = *data {}
| ^^^^^^^^^^^^^^^ ^^^^^
从编译器的角度来看,这些构造是否相同:
if let &Some(ref value) = data {
if let Some(ref value) = *data {
如果是这样,Clippy消息中的重点是什么,只是为了使用统一风格?
答案 0 :(得分:7)
是的,这些与编译器相同。在这个的情况下,没有太大的好处。真正的好处来自match
等价物:
fn func(data: &Foo) {
match data {
&Foo::One => {}
&Foo::Two => {}
&Foo::Three => {}
}
}
在这里,您只需要在模式中放置一个取消引用,而不是3个引用:
fn func(data: &Foo) {
match *data {
Foo::One => {}
Foo::Two => {}
Foo::Three => {}
}
}
从Rust 1.26开始,你甚至不需要取消引用匹配的表达式:
fn func(data: &Foo) {
match data {
Foo::One => {}
Foo::Two => {}
Foo::Three => {}
}
}
这就是为什么它是惯用的选择。
if let
概念只是其中的一个扩展。
您无法始终取消引用该值。如果你试图对一对项目做同样的事情:
fn func(data: &Foo, data2: &Foo) {
match (*data, *data2) {
(Foo::One, _) => {}
(Foo::Two, _) => {}
(Foo::Three, _) => {}
}
}
您收到错误
error[E0507]: cannot move out of borrowed content
--> src/main.rs:8:12
|
8 | match (*data, *data2) {
| ^^^^^ cannot move out of borrowed content
在这种情况下,您可以使用参考表格:
fn func(data: &Foo, data2: &Foo) {
match (data, data2) {
(&Foo::One, _) => {}
(&Foo::Two, _) => {}
(&Foo::Three, _) => {}
}
}
或者,自Rust 1.26起,执行一些隐式引用:
fn func(data: &Foo, data2: &Foo) {
match (data, data2) {
(Foo::One, x) => {}
(Foo::Two, _) => {}
(Foo::Three, _) => {}
}
}