BorrowCheck › Rust language core

Structs, enums, pattern matching

Structs, data-carrying enums, match exhaustiveness, refutability and pattern syntax.

The topic covers user-defined data types and pattern matching. The subtopic on structs covers structs with named fields, tuple structs and unit structs, field init shorthand, struct update syntax that moves fields, and the Debug requirement of {:?}. The subtopic on enums shows variants carrying data, discriminants of enums without data, a variant used as a constructor function, and why an Option<T> cannot be used as a T. The questions on match check exhaustiveness: why guards do not count toward it, why a match on an integer must cover the whole range, and what happens to an arm placed after _. The subtopics on refutability and pattern syntax examine which patterns are allowed in let and in function parameters, how @, .. and | alternatives work, and how matching through a reference avoids moving the value. The last subtopic is devoted to if let, while let, let else with its mandatory diverging else block, let chains in an if condition, and the matches! macro.

39 questions: 20 basic, 17 intermediate, 2 advanced. 6 info pages, 23 sources.

Subtopics

  1. Structs: named, tuple, unit
  2. Enums carrying data
  3. Exhaustiveness of match
  4. Refutability
  5. Pattern syntax: @, .., guards
  6. if let, while let, let else

Previous: Lifetimes, subtyping, variance · Next: Strings and text