Skip to content

Commit

Permalink
Use if let instead of match
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Feb 7, 2025
1 parent b607800 commit 7b9e5ed
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/pattern-matching/let-control-flow/let-else.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ off the end of the block).

```rust,editable
fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> {
let s = match maybe_string {
Some(s) => s,
None => return Err(String::from("got None")),
let s = if let Some(s) = maybe_string {
s
} else {
return Err(String::from("got None"));
};
let first_byte_char = if let Some(first) = s.chars().next() {
first
} else {
return Err(String::from("got empty string"));
};
let first_byte_char = match s.chars().next() {
Some(first) => first,
None => return Err(String::from("got empty string")),
let digit = if let Some(digit) = first_byte_char.to_digit(16) {
digit
} else {
return Err(String::from("not a hex digit"));
};
match first_byte_char.to_digit(16) {
Some(digit) => Ok(digit),
None => Err(String::from("not a hex digit")),
}
Ok(digit)
}
fn main() {
Expand Down

0 comments on commit 7b9e5ed

Please sign in to comment.