mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-03-23 15:14:35 +02:00
Simplify if-let example (#1977)
I find that `if let` makes the most sense to use when you don't have an `else` case, otherwise it's generally clearer to express the same thing with a `match`. This changes the `if let` example to be (arguably) a bit more idiomatic and less verbose.
This commit is contained in:
parent
d966750535
commit
04c28ed641
@ -18,14 +18,13 @@ The
|
|||||||
lets you execute different code depending on whether a value matches a pattern:
|
lets you execute different code depending on whether a value matches a pattern:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
fn sleep_for(secs: f32) {
|
fn sleep_for(secs: f32) {
|
||||||
let dur = if let Ok(dur) = std::time::Duration::try_from_secs_f32(secs) {
|
if let Ok(dur) = Duration::try_from_secs_f32(secs) {
|
||||||
dur
|
std::thread::sleep(dur);
|
||||||
} else {
|
println!("slept for {:?}", dur);
|
||||||
std::time::Duration::from_millis(500)
|
}
|
||||||
};
|
|
||||||
std::thread::sleep(dur);
|
|
||||||
println!("slept for {:?}", dur);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user