From 04c28ed64114e83cdff51a7cb2a10e3543b8e7a4 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Fri, 12 Apr 2024 09:45:01 -0700 Subject: [PATCH] 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. --- src/pattern-matching/let-control-flow.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/pattern-matching/let-control-flow.md b/src/pattern-matching/let-control-flow.md index 98cac6da..efbcadc9 100644 --- a/src/pattern-matching/let-control-flow.md +++ b/src/pattern-matching/let-control-flow.md @@ -18,14 +18,13 @@ The lets you execute different code depending on whether a value matches a pattern: ```rust,editable +use std::time::Duration; + fn sleep_for(secs: f32) { - let dur = if let Ok(dur) = std::time::Duration::try_from_secs_f32(secs) { - dur - } else { - std::time::Duration::from_millis(500) - }; - std::thread::sleep(dur); - println!("slept for {:?}", dur); + if let Ok(dur) = Duration::try_from_secs_f32(secs) { + std::thread::sleep(dur); + println!("slept for {:?}", dur); + } } fn main() {