-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3702 from veera-sivarajan/add-letElse
Introduce `let`-`else` statement
- Loading branch information
Showing
12 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-07/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-07/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "enums" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
45 changes: 45 additions & 0 deletions
45
listings/ch06-enums-and-pattern-matching/listing-06-07/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[derive(Debug)] // so we can inspect the state in a minute | ||
enum UsState { | ||
Alabama, | ||
Alaska, | ||
// --snip-- | ||
} | ||
|
||
// ANCHOR: state | ||
impl UsState { | ||
fn existed_in(&self, year: u16) -> bool { | ||
match self { | ||
UsState::Alabama => year >= 1819, | ||
UsState::Alaska => year >= 1959, | ||
// -- snip -- | ||
} | ||
} | ||
} | ||
// ANCHOR_END: state | ||
|
||
enum Coin { | ||
Penny, | ||
Nickel, | ||
Dime, | ||
Quarter(UsState), | ||
} | ||
|
||
// ANCHOR: describe | ||
fn describe_state_quarter(coin: Coin) -> Option<String> { | ||
if let Coin::Quarter(state) = coin { | ||
if state.existed_in(1900) { | ||
Some(format!("{state:?} is pretty old, for America!")) | ||
} else { | ||
Some(format!("{state:?} is relatively new.")) | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
// ANCHOR_END: describe | ||
|
||
fn main() { | ||
if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) { | ||
println!("{desc}"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-08/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-08/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "enums" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
45 changes: 45 additions & 0 deletions
45
listings/ch06-enums-and-pattern-matching/listing-06-08/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[derive(Debug)] // so we can inspect the state in a minute | ||
enum UsState { | ||
Alabama, | ||
Alaska, | ||
// --snip-- | ||
} | ||
|
||
impl UsState { | ||
fn existed_in(&self, year: u16) -> bool { | ||
match self { | ||
UsState::Alabama => year >= 1819, | ||
UsState::Alaska => year >= 1959, | ||
// -- snip -- | ||
} | ||
} | ||
} | ||
|
||
enum Coin { | ||
Penny, | ||
Nickel, | ||
Dime, | ||
Quarter(UsState), | ||
} | ||
|
||
// ANCHOR: describe | ||
fn describe_state_quarter(coin: Coin) -> Option<String> { | ||
let state = if let Coin::Quarter(state) = coin { | ||
state | ||
} else { | ||
return None; | ||
}; | ||
|
||
if state.existed_in(1900) { | ||
Some(format!("{state:?} is pretty old, for America!")) | ||
} else { | ||
Some(format!("{state:?} is relatively new.")) | ||
} | ||
} | ||
// ANCHOR_END: describe | ||
|
||
fn main() { | ||
if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) { | ||
println!("{desc}"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-09/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
listings/ch06-enums-and-pattern-matching/listing-06-09/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "enums" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
43 changes: 43 additions & 0 deletions
43
listings/ch06-enums-and-pattern-matching/listing-06-09/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#[derive(Debug)] // so we can inspect the state in a minute | ||
enum UsState { | ||
Alabama, | ||
Alaska, | ||
// --snip-- | ||
} | ||
|
||
impl UsState { | ||
fn existed_in(&self, year: u16) -> bool { | ||
match self { | ||
UsState::Alabama => year >= 1819, | ||
UsState::Alaska => year >= 1959, | ||
// -- snip -- | ||
} | ||
} | ||
} | ||
|
||
enum Coin { | ||
Penny, | ||
Nickel, | ||
Dime, | ||
Quarter(UsState), | ||
} | ||
|
||
// ANCHOR: describe | ||
fn describe_state_quarter(coin: Coin) -> Option<String> { | ||
let Coin::Quarter(state) = coin else { | ||
return None; | ||
}; | ||
|
||
if state.existed_in(1900) { | ||
Some(format!("{state:?} is pretty old, for America!")) | ||
} else { | ||
Some(format!("{state:?} is relatively new.")) | ||
} | ||
} | ||
// ANCHOR_END: describe | ||
|
||
fn main() { | ||
if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) { | ||
println!("{desc}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters