mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-16 05:24:17 -06:00
finished ch 6.2
This commit is contained in:
parent
f7f6bc84bb
commit
0b7fa1299b
16
.obsidian/workspace.json
vendored
16
.obsidian/workspace.json
vendored
@ -179,15 +179,15 @@
|
|||||||
},
|
},
|
||||||
"active": "bda58467a9a0a34e",
|
"active": "bda58467a9a0a34e",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"README.md",
|
|
||||||
"data_types.md",
|
|
||||||
"Untitled.canvas",
|
|
||||||
"ownership.md",
|
|
||||||
"Variables.md",
|
|
||||||
"Data Types.md",
|
"Data Types.md",
|
||||||
"Constants.md",
|
"data_types.md",
|
||||||
|
"Primitives.md",
|
||||||
|
"ownership.md",
|
||||||
"Enums.md",
|
"Enums.md",
|
||||||
"Structures.md",
|
"Variables.md",
|
||||||
"Primitives.md"
|
"README.md",
|
||||||
|
"Untitled.canvas",
|
||||||
|
"Constants.md",
|
||||||
|
"Structures.md"
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -851,3 +851,69 @@ then it would print out State quarter from Alaska
|
|||||||
because that is the only pattern that matches
|
because that is the only pattern that matches
|
||||||
|
|
||||||
## Matching with Option T
|
## Matching with Option T
|
||||||
|
|
||||||
|
this can be done
|
||||||
|
|
||||||
|
here is an example
|
||||||
|
where there is a function that adds 1 to a non-null type and returns a null type if it is null
|
||||||
|
```rust
|
||||||
|
fn plus_one(x: Option<i32>) -> Option<i32> {
|
||||||
|
match x {
|
||||||
|
None => None, // the null part
|
||||||
|
// i binds to the value inside Option i32
|
||||||
|
Some(i) => Some(i + 1), // the non-null part
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let five = Some(5);
|
||||||
|
let six = plus_one(five);
|
||||||
|
let none = plus_one(None);
|
||||||
|
```
|
||||||
|
|
||||||
|
this handles both the None case and the not-null case
|
||||||
|
|
||||||
|
very common pattern in rust to combine enums and matches then binding to the value inside
|
||||||
|
|
||||||
|
very useful for when a range of data types are included
|
||||||
|
|
||||||
|
## Matches are Exhaustive
|
||||||
|
Matches must cover all possibilities
|
||||||
|
there must be an arm for each pattern that could possibly come into contact with it
|
||||||
|
if you don't he compiler will give an error
|
||||||
|
|
||||||
|
all matches must exist therefore making matches exhaustive by default
|
||||||
|
|
||||||
|
## Catch All Patterns and _ Placeholder
|
||||||
|
|
||||||
|
for example lets say you have a game where if you roll a 3 you add a fancy hat and if you roll a 7 you remove a fancy hat and all other rolls result in the player moving
|
||||||
|
|
||||||
|
it would look something like this
|
||||||
|
```rust
|
||||||
|
let dice_roll = 9;
|
||||||
|
match dice_roll {
|
||||||
|
// can do specific numbers as the pattern
|
||||||
|
3 => add_fancy_hat(),
|
||||||
|
7 => remove_fancy_hat(),
|
||||||
|
// this is allowed as a catch all this matches against all patterns not listed
|
||||||
|
// should go last in the case that it matches all patterns (including 3 and 7)
|
||||||
|
other => move_player(other),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_fancy_hat() {}
|
||||||
|
fn remove_fancy_hat() {}
|
||||||
|
fn move_player(num_spaces: u8) {}
|
||||||
|
```
|
||||||
|
this uses a variable named other not a keyword
|
||||||
|
this will match
|
||||||
|
this catch all of other will satisfy the exhaustive requirements of match
|
||||||
|
catch all must be last, rust will give a warning if this the wrong order and it will never run
|
||||||
|
|
||||||
|
if we want to catch all but not use the value do _
|
||||||
|
this matches to all values and does not blind to that value
|
||||||
|
this is like ignoring all other values not found in this arm
|
||||||
|
|
||||||
|
if you want nothing to happen from the ignore all then return a unit type from the match
|
||||||
|
|
||||||
|
for example the arm _ => () does this, this should not be used in the case of a match trying to return something
|
||||||
|
|
||||||
|
# If Let
|
Loading…
x
Reference in New Issue
Block a user