Skip to content

Patterns

Patterns nest: Shape.Line(Point { x: 0, y: 0 }, end), Some((n, name)), Shape.Label(name, Some(n)). A struct pattern names the fields it checks and ends with .. if it leaves any out. Inside a pattern a plain name binds that part; at the top of an arm it binds the whole value. A guard adds a condition: n if n > 10 => ...; it sees the arm’s bindings (values that own memory by reference, so a guard cannot move them) and a guarded arm does not count toward covering the cases. A match must cover every value; when it does not, the error shows the missing ones, like Shape.Pair(false, false) or P { a: false, b: _ }.

Text can be matched by its start and end: "GET " + path => ... matches text starting with GET and binds the rest as a new Str; name + ".txt" matches the end, and "<" + inner + ">" both. The text matched is not moved by this.

Where the enum is already known, a variant can drop the enum’s name: .Circle(r) => ... in a match on a Shape, and .Circle(2.0) or .Empty wherever a Shape is expected (a typed let, an argument, a return, an assignment, a list of shapes, the right side of ==). Without a known type, .Circle is an error asking for the name.