|
| 1 | +========== |
| 2 | +Patterns |
| 3 | +========== |
| 4 | + |
| 5 | +-------------------- |
| 6 | +What Is a Pattern? |
| 7 | +-------------------- |
| 8 | + |
| 9 | +- Core language feature of Rust |
| 10 | + |
| 11 | +- Describes the **structure** of a value |
| 12 | + |
| 13 | +- Used to *test* and *decompose* values |
| 14 | + |
| 15 | +- Successful matching may introduce new bindings |
| 16 | + |
| 17 | +**Basic Pattern** |
| 18 | + |
| 19 | +.. code:: rust |
| 20 | +
|
| 21 | + let musketeers = 3; |
| 22 | +
|
| 23 | + // Pattern: Identifier (musketeers) |
| 24 | + // Structure: Single, scalar value |
| 25 | + // Binding: 'musketeers' is now bound to value '3' |
| 26 | +
|
| 27 | +-------------------------- |
| 28 | +Patterns Are Declarative |
| 29 | +-------------------------- |
| 30 | + |
| 31 | +- Patterns describe *what* (shape) rather than *how* (steps) |
| 32 | + |
| 33 | +- Matching is a structural check |
| 34 | + |
| 35 | + - Not a sequence of procedural field accesses |
| 36 | + |
| 37 | +- Scale effectively as data structures grow in complexity |
| 38 | + |
| 39 | +.. code:: rust |
| 40 | +
|
| 41 | + // Declarative: Describing the 'stencil' |
| 42 | + match point { |
| 43 | + Point { x: 0, y: ver } => println!("On Y axis at {ver}"), |
| 44 | + _ => {} |
| 45 | + } |
| 46 | +
|
| 47 | + // Procedural: Step-by-step instructions (what patterns avoid) |
| 48 | + if point.x == 0 { |
| 49 | + let ver = point.y; |
| 50 | + println!("On Y axis at {ver}"); |
| 51 | + } |
| 52 | +
|
| 53 | +---------------------- |
| 54 | +Patterns as Bindings |
| 55 | +---------------------- |
| 56 | + |
| 57 | +- Every :rust:`let` binding uses a pattern |
| 58 | + |
| 59 | +- Simple bindings use identifier patterns |
| 60 | + |
| 61 | +- Complex patterns can destructure values |
| 62 | + |
| 63 | +.. code:: rust |
| 64 | +
|
| 65 | + let number = 5; // identifier pattern |
| 66 | +
|
| 67 | + let (first, second) = (1, 2); // tuple pattern |
| 68 | +
|
| 69 | +------------------ |
| 70 | +Literal Patterns |
| 71 | +------------------ |
| 72 | + |
| 73 | +- Match exact values |
| 74 | + |
| 75 | +- Commonly used in :rust:`match` expressions |
| 76 | + |
| 77 | +- Useful for branching on specific cases |
| 78 | + |
| 79 | +.. code:: rust |
| 80 | +
|
| 81 | + let choices = 3; |
| 82 | +
|
| 83 | + match choices { |
| 84 | + 0 => println!("zero"), |
| 85 | + 1 => println!("one"), |
| 86 | + _ => println!("too many"), |
| 87 | + } |
| 88 | +
|
| 89 | +------------------ |
| 90 | +Wildcard Pattern |
| 91 | +------------------ |
| 92 | + |
| 93 | +- :rust:`_` matches any value |
| 94 | + |
| 95 | +- Does not *bind* or *move* the value |
| 96 | + |
| 97 | +- Often used to ignore irrelevant cases |
| 98 | + |
| 99 | +.. code:: rust |
| 100 | +
|
| 101 | + enum Status { |
| 102 | + Ok(i32), |
| 103 | + Error, |
| 104 | + } |
| 105 | +
|
| 106 | + let status = Status::Ok(10); |
| 107 | +
|
| 108 | + match status { |
| 109 | + Status::Ok(_) => println!("ok"), |
| 110 | + Status::Error => println!("error"), |
| 111 | + } |
| 112 | +
|
| 113 | +----------------------- |
| 114 | +Binding with Patterns |
| 115 | +----------------------- |
| 116 | + |
| 117 | +- Identifier patterns bind matched values to names |
| 118 | + |
| 119 | +- Bindings only exist when the pattern matches |
| 120 | + |
| 121 | +- Commonly used with enums and tuples |
| 122 | + |
| 123 | +.. code:: rust |
| 124 | +
|
| 125 | + let (first, second) = (10, 20); |
| 126 | +
|
| 127 | + println!("first is {}, second is {}", first, second); |
| 128 | +
|
| 129 | +--------------------- |
| 130 | +Pattern Composition |
| 131 | +--------------------- |
| 132 | + |
| 133 | +- Patterns may be composed recursively |
| 134 | + |
| 135 | +- Larger patterns are built from smaller ones |
| 136 | + |
| 137 | +- Inner patterns describe substructure |
| 138 | + |
| 139 | +.. code:: rust |
| 140 | + :number-lines: 1 |
| 141 | +
|
| 142 | + let point = (0, 5); |
| 143 | +
|
| 144 | + match point { |
| 145 | + (0, y) => println!("on y-axis at {}", y), |
| 146 | + _ => {} |
| 147 | + } |
| 148 | +
|
| 149 | +:command:`on y-axis at 5` |
| 150 | + |
| 151 | +- If line 1 was :rust:`let point = (5, 0);` |
| 152 | + |
| 153 | + - Fails to match the first pattern because 5 does not equal 0 |
| 154 | + - Matches the wildcard pattern (:rust:`_`) |
| 155 | + |
| 156 | + - Performs no action |
| 157 | + - Nothing is printed |
| 158 | + |
| 159 | +-------------------- |
| 160 | +Pattern Vocabulary |
| 161 | +-------------------- |
| 162 | + |
| 163 | +- **Literal Matching:** Patterns can match specific values like numbers or strings |
| 164 | + |
| 165 | +- **Alternative Patterns:** Use the "pipe" (:rust:`|`) to handle multiple values in a single arm |
| 166 | + |
| 167 | +- **Variable Bindings:** Use :rust:`@` to give a name to a value while checking it against a range or pattern |
| 168 | + |
| 169 | +- **The Rest Pattern:** A placeholder (:rust:`..`) that ignores "everything else" in a sequence or structure |
| 170 | + |
| 171 | +.. code:: rust |
| 172 | +
|
| 173 | + let x = 5; |
| 174 | +
|
| 175 | + match x { |
| 176 | + // Matches 1, 2, or 3 |
| 177 | + 1 | 2 | 3 => println!("Small"), |
| 178 | + |
| 179 | + // Binds the value to 'n' AND checks the range |
| 180 | + n @ 4..=10 => println!("Value {n} is in range"), |
| 181 | + |
| 182 | + _ => println!("Other"), |
| 183 | + } |
| 184 | +
|
| 185 | +----------------------------- |
| 186 | +Patterns in Rust Constructs |
| 187 | +----------------------------- |
| 188 | + |
| 189 | +Patterns are reused consistently across the language |
| 190 | + |
| 191 | +- :rust:`let` bindings |
| 192 | + |
| 193 | +- :rust:`match` expressions |
| 194 | + |
| 195 | +- :rust:`if let` and :rust:`while let` |
| 196 | + |
| 197 | +- function parameters (covered in another module) |
0 commit comments