finished ch6

This commit is contained in:
darkicewolf50 2025-01-08 16:29:25 -07:00
parent 00c0962826
commit 2bb6f5f969
3 changed files with 53 additions and 4 deletions

View File

@ -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",

1
Modules.md Normal file
View File

@ -0,0 +1 @@
# Modules

View File

@ -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
# 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<u8> 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