Why can't we allow conditions in the if-let?

It would be nice to have this code working, is not it?

if let Some(time_string) = servak.time_string() && !time_string.is_empty() {
    f = f.field("Time", &time_string, true);
}

Now I need to write noisy code:

if let Some(time_string) = servak.time_string() {
    if !time_string.is_empty() {
        f = f.field("Time", &time_string, true);
    }
}

There is discussions on how to improve expressiveness of if-let patterns, but nothing concrete yet.

2 Likes

Note that in the meantime you can reduce excess indentation using https://crates.io/crates/if_chain

if_chain! {
    if let Some(time_string) = servak.time_string();
    if !time_string.is_empty();
    then {
        f = f.field("Time", &time_string, true);
    }
}

(Please excuse any typos in the above; I didn't compile it.)