docs

#E0102: 或模式的各个选项绑定不同

a | b 的每个选项都要以相同的类型绑定相同的名称。在每个选项中绑定相同的名称,或者给绑定一个和类型。

nabi
enum Shape { Circle(f64), Square(f64), Point }

fn size(s: Shape) -> f64 {
    match s {
        Shape::Circle(r) | Shape::Point => r,
        Shape::Square(w) => w,
    }
}
error[E0102]: or-pattern alternatives bind different name sets (alternative 1: missing `r`)
  --> main.nabi:5:28
   |
 5 |         Shape::Circle(r) | Shape::Point => r,
   |                            ^^^^^

在每个选项中绑定相同的名称:

nabi
enum Shape { Circle(f64), Square(f64), Point }

fn size(s: Shape) -> f64 {
    match s {
        Shape::Circle(v) | Shape::Square(v) => v,
        Shape::Point => 0.0,
    }
}
println(f"{size(Shape::Square(2.0))}");
2

相关错误码:E0098 不穷尽的 match,E0103 看起来像变体的绑定

发现错误? 在 GitHub 上编辑此页 最后更新: