Lou*_*inn 3 language-lawyer rust
我正在查看 Rust 参考书,它指出元组结构“当其子模式之一可反驳时,它是可反驳的”。它还指出,当“未指定子模式”时,标识符模式是无可辩驳的。
鉴于此,如果我使用单个不可反驳的标识符子模式创建一个元组结构模式,我希望元组结构模式也是不可反驳的,因为它的子模式都是不可反驳的。
pub fn main() {
enum Foo {
Bar(i32),
Baz(i32),
}
// Compiler says Foo::Bar(x) is a refutable pattern
// but the reference book suggests it is irrefutable
let Foo::Bar(x) = Foo::Bar(50);
}
Run Code Online (Sandbox Code Playgroud)
上面代码的 AST 确认 是Foo::Bar(x)一个元组结构模式并且x是一个标识符模式。我觉得编译器在这里是正确的,但参考书表明Foo::Bar(x)尽管编译器是无可辩驳的。
我理解考虑到多种可能的枚举变体需要阻止可反驳的模式,以及 anif let或 like 在这里有何帮助,但对相互矛盾的引号感到困惑。
这是我遗漏的事情,还是参考书的问题?
的类型Foo::Bar(50)是Foo,就像 a 的类型4一样i32。
看这个:
let 4 = 4;
Run Code Online (Sandbox Code Playgroud)
error[E0005]: refutable pattern in local binding: `i32::MIN..=3_i32` and `5_i32..=i32::MAX` not covered
--> src/main.rs:2:9
|
2 | let 4 = 4;
| ^ patterns `i32::MIN..=3_i32` and `5_i32..=i32::MAX` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
Run Code Online (Sandbox Code Playgroud)
这是可以反驳的,因为右边的类型是。该值与可反驳性无关。这都是关于类型的。4i32
并且let 4 = <i32>是可反驳的,因为<i32>可能是与 不同的值4。
我认为refutability只讲类型,不讲价值观。
当然,我不是理论语言专家,所以我可能完全不同意。但这就是我对可反驳性含义的理解。
| 归档时间: |
|
| 查看次数: |
382 次 |
| 最近记录: |