Does a future await inside a conditional is lazyly evaluated?

Does awaiting a future inside a conditional expression always resolves the future even if it would not be required to compute the logical expression?

For example:

async fn get_bool() -> bool {
    todo!("the implementation is not relevant")
}

async fn do_the_thing(allowed: bool) {
    if allowed && get_bool().await {
        todo!("do the thing")
    }
}

So if allowed is false, the value of get_bool is not relevant to the if expression.
Does the future generated by get_bool is resolved in this case?

Since get_bool is not called (conditional expressions in Rust, like most languages, are short circuiting), no Future is returned by get_bool. So there is nothing to be resolved.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.