Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

match

Normal

match x:
    case 10:
        # ...
    case 20:
        # ...
    case _:
        # ...

String

match greeting:
    case "hi":
        # ...
    case "hello":
        # ...
    case _:
        # ...

Grouping

match x:
    case 10 | 20 | 30:
        # ...
    case _:
        # ...

Mit if statement

match x:
    case x_even if x % 2 == 0:
        # ...
    case _:
        # ...

Listen und Dictionaries lernen wir erst in den folgenden Kapiteln. TODO: Verschiebe es und erkläre Patternmatching hier

Mit Listen

match my_list:
    case [a, b, c]:
        # ...
    case [a, b, c, d]:
        # ...
    case _:
        # ...

Mit Dictionaries

match my_dict:
    case { "name": name, "age": age }:
        # ...
    case _:
        # ...