E3009

E3009#

记录模式不能只包含 ..。请改用通配符模式 _

错误示例#

///|
struct Point {
  x : Int
  y : Int
}

///|
fn process_point(p : Point) -> Unit {
  match p {
    { .. } => println("Got a point")
    //^~~~~~
    // Error: Struct pattern cannot contain only `..`, use wildcard pattern `_` instead.
  }
}

建议#

使用通配符模式 _ 代替 { .. }

///|
pub(all) struct Point {
  x : Int
  y : Int
}

///|
fn process_point(p : Point) -> Unit {
  match p {
    _ => println("Got a point")
  }
}

///|
test {
  let p : Point = { x: 0, y: 1 }
  process_point(p)
  process_y_axis(p)
}

如果你想匹配特定字段的话,你也可以与其他字段一起使用 { .. }

///|
fn process_y_axis(p : Point) -> Unit {
  match p {
    { x: 0, .. } => println("Point on y-axis")
    _ => println("Other point")
  }
}