这种行为背后的原因是什么?y在引擎盖下面有阴影吗?
是的,这引入了一个新的绑定
y
这将匹配任何东西。此代码会产生警告,指示两者
y
使用了:
warning: unused variable: `y`
--> src/main.rs:3:9
|
3 | let y = Some(2);
| ^ help: if this is intentional, prefix it with an underscore: `_y`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `y`
--> src/main.rs:4:30
|
4 | println!("{}",matches!(x,y));
| ^ help: if this is intentional, prefix it with an underscore: `_y`
这类似于
Why is this match pattern unreachable when using non-literal patterns?
但形式略有不同,
matches!(x,y)
相当于此(请参见
source
对于
matches!
):
match x {
y => true,
_ => false,
}
标识符用于在模式中创建新绑定(除非它们是常量),因此不使用具有该名称的变量。