Skip to content

Commit 088744a

Browse files
authored
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.
1 parent 40652b6 commit 088744a

File tree

1 file changed

+3
-1
lines changed
  • src/pattern-matching/let-control-flow

1 file changed

+3
-1
lines changed

src/pattern-matching/let-control-flow/if-let.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ lets you execute different code depending on whether a value matches a pattern:
88
use std::time::Duration;
99
1010
fn sleep_for(secs: f32) {
11-
if let Ok(duration) = Duration::try_from_secs_f32(secs) {
11+
let result = Duration::try_from_secs_f32(secs);
12+
13+
if let Ok(duration) = result {
1214
std::thread::sleep(duration);
1315
println!("slept for {duration:?}");
1416
}

0 commit comments

Comments
 (0)