E3009#
结构体模式不能仅包含 ..
,请使用通配符模式 _
。
错误示例#
struct Point {
x: Int
y: Int
}
fn process_point(p: Point) -> Unit {
match p {
{ .. } => println("Got a point")
//^~~~~~
// Error: Record pattern cannot contain only `..`, use wildcard pattern `_` instead.
}
}
建议#
使用通配符模式 _
代替 { .. }
:
struct Point {
x: Int
y: Int
}
fn process_point(p: Point) -> Unit {
match p {
_ => println("Got a point")
}
}
如果你想匹配特定字段的话,你也可以与其他字段一起使用 { .. }
:
fn process_point(p: Point) -> Unit {
match p {
{ x: 0, .. } => println("Point on y-axis")
_ => println("Other point")
}
}