From 0b7fa1299beb5aa4b2e53b2db0aa14ea018a5f6e Mon Sep 17 00:00:00 2001 From: darkicewolf50 Date: Wed, 8 Jan 2025 15:31:15 -0700 Subject: [PATCH] finished ch 6.2 --- .obsidian/workspace.json | 16 +++++----- data_types.md | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index a774529..a168acb 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -179,15 +179,15 @@ }, "active": "bda58467a9a0a34e", "lastOpenFiles": [ - "README.md", - "data_types.md", - "Untitled.canvas", - "ownership.md", - "Variables.md", "Data Types.md", - "Constants.md", + "data_types.md", + "Primitives.md", + "ownership.md", "Enums.md", - "Structures.md", - "Primitives.md" + "Variables.md", + "README.md", + "Untitled.canvas", + "Constants.md", + "Structures.md" ] } \ No newline at end of file diff --git a/data_types.md b/data_types.md index 1b57edf..7ba15e6 100644 --- a/data_types.md +++ b/data_types.md @@ -851,3 +851,69 @@ then it would print out State quarter from Alaska because that is the only pattern that matches ## 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) -> Option { + 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 \ No newline at end of file