Introducing patterns

Patterns provide rules to match values. You can use patterns in switch cases, as well as in if, while, guard, and for statements. You can also use patterns in variable and constant declarations.

Believe it or not, you’ve already seen another good example of patterns with that coordinate tuple declaration. You construct a tuple by separating values with commas between parentheses, like (x, y, z). The compiler will understand that pattern is referring to a tuple of 3 values: x, y and z. Tuples have the structure of a composite value.

Single values also have a structure. The number 42 is a single value and by its very nature is identifiable.

The structure of a value is defined by a pattern. Pattern matching lets you check values against each other.

Note: The structure of a value is not referring to the struct type. They are different concepts even though we use the same word in English. That's the paucity of language!

results matching ""

    No results matching ""