if_chain icon indicating copy to clipboard operation
if_chain copied to clipboard

Support while let

Open omjadas opened this issue 2 years ago • 0 comments

I would find it useful for if_chain to also support while let.

if_chain! {
    if let Some(b) = a;
    while let Ok(c) = b;
    if let Some(d) = c;
    then {
        do_something_with_d(d);
    }
}

becomes:

if let Some(b) = a {
    while let Ok(c) = b {
        if let Some(d) = c {
            do_something_with_d(d);
        }
    }
}

I'm not sure how/if this should work with the else block. Perhaps something like the following could be done.

if_chain! {
    if let Some(b) = a;
    while let Ok(c) = b;
    if let Some(d) = c;
    then {
        do_something_with_d(d);
    } else {
        do_something_else();
    }
}

becomes:

if let Some(b) = a {
    let mut __executed = false;
    while let Ok(c) = b {
        __executed = true;
        if let Some(d) = c {
            do_something_with_d(d);
        } else {
            do_something_else();
        }
    }
    if !__executed {
        do_something_else();
    }
} else {
    do_something_else();
}

omjadas avatar Nov 20 '21 01:11 omjadas