From 2bb6f5f969d06b3b15e0209fd45af58d65d3815d Mon Sep 17 00:00:00 2001 From: darkicewolf50 Date: Wed, 8 Jan 2025 16:29:25 -0700 Subject: [PATCH] finished ch6 --- .obsidian/workspace.json | 22 +++++++++++++++++++--- Modules.md | 1 + data_types.md | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 Modules.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index a168acb..3c720f3 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -21,6 +21,20 @@ "title": "data_types" } }, + { + "id": "b80f5219fa24358f", + "type": "leaf", + "state": { + "type": "markdown", + "state": { + "file": "Modules.md", + "mode": "source", + "source": false + }, + "icon": "lucide-file", + "title": "Modules" + } + }, { "id": "bc05904661f131c7", "type": "leaf", @@ -35,7 +49,8 @@ "title": "Constants" } } - ] + ], + "currentTab": 1 } ], "direction": "vertical" @@ -177,10 +192,11 @@ "command-palette:Open command palette": false } }, - "active": "bda58467a9a0a34e", + "active": "b80f5219fa24358f", "lastOpenFiles": [ - "Data Types.md", "data_types.md", + "Modules.md", + "Data Types.md", "Primitives.md", "ownership.md", "Enums.md", diff --git a/Modules.md b/Modules.md new file mode 100644 index 0000000..016d434 --- /dev/null +++ b/Modules.md @@ -0,0 +1 @@ +# Modules \ No newline at end of file diff --git a/data_types.md b/data_types.md index 7ba15e6..a1fe3da 100644 --- a/data_types.md +++ b/data_types.md @@ -916,4 +916,36 @@ if you want nothing to happen from the ignore all then return a unit type from t 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 +# Concise Flow Control with if let + +This is a concise way of handling the case where there is only 1 arm and the rest are ignored using _ + +this would look like +```rust + let config_max = Some(3u8); // Option is the value owned by the variable + match config_max { + Some(max) => println!("The maximum is configured to be {max}"), + _ => (), // annoying boilerplate + } +``` + +here is how to remove the boilerplate with a if let +this behaves just like the match above +```rust + let config_max = Some(3u8); + if let Some(max) = config_max { + println!("The maximum is configured to be {max}"); + } +``` + +if let is separated by an = +the max binds to the value in option u8 then max can be used in the body of the if let + +the block doesn't run if the pattern doesn't match exactly +this reduces the amount of typing and boilerplate but you lose the exhaustive nature of match which is enforced + +if let is a good refactor to when conciseness is preferred and working module/function is "done" + +should be used when only matching against one pattern and only EVER ONE pattern + +good to get rid of too verbose code