Binding

Indirectly accessing a variable makes it impossible to branch and use that
variable without re-binding. match provides the @ sigil for binding values to
names:

  1. // A function `age` which returns a `u32`.
  2. fn age() -> u32 {
  3. 15
  4. }
  5. fn main() {
  6. println!("Tell me type of person you are");
  7. match age() {
  8. 0 => println!("I'm not born yet I guess"),
  9. // Could `match` 1 ... 12 directly but then what age
  10. // would the child be? Instead, bind to `n` for the
  11. // sequence of 1 .. 12. Now the age can be reported.
  12. n @ 1 ... 12 => println!("I'm a child of age {:?}", n),
  13. n @ 13 ... 19 => println!("I'm a teen of age {:?}", n),
  14. // Nothing bound. Return the result.
  15. n => println!("I'm an old person of age {:?}", n),
  16. }
  17. }

See also:

functions