From 088744a4c23d679d5c611248f488e20c3ca683b7 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Thu, 26 Jun 2025 12:01:46 -0700 Subject: [PATCH] Split off function call to make pattern match clearer (#2785) I think doing the call to `Duration::try_from_secs_f32` in the pattern match is a bit verbose since the function name is so long. I usually split this off into a separate variable so that it's easier to see the pattern match syntax. --- src/pattern-matching/let-control-flow/if-let.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pattern-matching/let-control-flow/if-let.md b/src/pattern-matching/let-control-flow/if-let.md index 2f5717f2..d9257069 100644 --- a/src/pattern-matching/let-control-flow/if-let.md +++ b/src/pattern-matching/let-control-flow/if-let.md @@ -8,7 +8,9 @@ lets you execute different code depending on whether a value matches a pattern: use std::time::Duration; fn sleep_for(secs: f32) { - if let Ok(duration) = Duration::try_from_secs_f32(secs) { + let result = Duration::try_from_secs_f32(secs); + + if let Ok(duration) = result { std::thread::sleep(duration); println!("slept for {duration:?}"); }