E0123
A pattern written another language's way
ErrorUnsafe fixA pattern takes a variant apart by position: Rect(x, y) binds the first field to x and the second to y. It takes no labels, as in Rect(w: x, h: y), and no braces, as in Rust's Rect { w, h }. It takes no type arguments, as in Option<i64>.Some(v): the type of the matched value gives them.
Why
A pattern lists one sub-pattern for each field, in the order the variant declares them, so a label adds nothing. The fix is unsafe: labels written out of order would swap the bindings when deleted. The fix for type arguments is safe: it deletes them, and the type of the matched value then gives them.
Broken
enum Shape {
Rect(w: i64, h: i64)
}
fn area(s: Shape) -> i64 {
match s {
Rect(w: x, h: y) => x * y
}
}
fn main() {
print(area(Shape.Rect(w: 2, h: 3)))
}
What sisu says
error[E0123]: a pattern binds by position: `Rect(x, y)` --> E0123.sisu:7:14 | 7 | Rect(w: x, h: y) => x * y | ^ | help: remove the labels | 7 - Rect(w: x, h: y) => x * y 7 + Rect(x, y) => x * y | sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0123
Fixed
enum Shape {
Rect(w: i64, h: i64)
}
fn area(s: Shape) -> i64 {
match s {
Rect(x, y) => x * y
}
}
fn main() {
print(area(Shape.Rect(w: 2, h: 3)))
}
Sisu