3 Indirectly accessing a variable makes it impossible to branch and use that
4 variable without re-binding. `match` provides the `@` sigil for binding values to
8 // A function `age` which returns a `u32`.
14 println!("Tell me what type of person you are");
17 0 => println!("I haven't celebrated my first birthday yet"),
18 // Could `match` 1 ..= 12 directly but then what age
19 // would the child be? Instead, bind to `n` for the
20 // sequence of 1 ..= 12. Now the age can be reported.
21 n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
22 n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
23 // Nothing bound. Return the result.
24 n => println!("I'm an old person of age {:?}", n),
29 You can also use binding to "destructure" `enum` variants, such as `Option`:
32 fn some_number() -> Option<u32> {
38 // Got `Some` variant, match if its value, bound to `n`,
40 Some(n @ 42) => println!("The Answer: {}!", n),
41 // Match any other number.
42 Some(n) => println!("Not interesting... {}", n),
43 // Match anything else (`None` variant).
50 [`functions`][functions], [`enums`][enums] and [`Option`][option]
52 [functions]: ../../fn.md
53 [enums]: ../../custom_types/enum.md
54 [option]: ../../std/option.md