Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does if-let with a pair short-circuit?

Suppose f() and g() return Option<T>.

if let (Some(x), Some(y)) = (f(), g()) {
    h();
}

If f() returns None, will g() be evaluated? Is evaluation guaranteed or prohibited by the spec?

like image 936
Mario Carneiro Avatar asked Jun 30 '16 08:06

Mario Carneiro


1 Answers

It does not shortcut. To pattern-match against the pair, the pair must be fully constructed, which means both f and g have been called. There is no lazy evaluation where the pattern match could happen before the values are calculated.

(By the way, it's easy to try out, and the Rust compiler pretty much is the spec right now.)

like image 115
Sebastian Redl Avatar answered Sep 18 '22 22:09

Sebastian Redl