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


#1

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);
    }
}

#2

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


#3

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.)