At the moment one of the limitations of pattern matching is the inability to use variables in the scope of the match.
pcwalton suggests using const:
const origin: Point = Point { x: 0, y: 0 };
let foo = Point { x: 12, y: 42 }
match p {
const origin => io::println(~"the point lies at the origin"),
Point { origin.x, y } => io::println(fmt!("the point lies on the x axis, at y=%?", y)),
Point { x, origin.y } => io::println(fmt!("the point lies on the y axis, at x=%?", x)),
const foo => io::println(~"the point is at the point \"foo\""),
Point { x, y } => io::println(fmt!("The point is at (%?, %?)", x, y))
}
nmatsakis suggests using the == operator:
const origin: Point = Point { x: 0, y: 0 };
let foo = Point { x: 12, y: 42 }
match p {
== origin => io::println(~"the point lies at the origin"),
Point { origin.x, y } => io::println(fmt!("the point lies on the x axis, at y=%?", y)),
Point { x, origin.y } => io::println(fmt!("the point lies on the y axis, at x=%?", x)),
== foo => io::println(~"the point is at the point \"foo\""),
Point { x, y } => io::println(fmt!("The point is at (%?, %?)", x, y))
}
How about something that looks like capture clauses: