docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#E0102: or-pattern alternatives bind differently

Every alternative of a | b binds the same names, with the same types. Bind the same names in each, or give the binding a sum type.

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,
   |                            ^^^^^

Bind the same names in every alternative:

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

Related: E0098 non-exhaustive match, E0103 binding looks like a variant

Found a mistake? Edit this page on GitHub. Last updated