Swift-like guard statement

Wrapping that up in a (possibly?) easier on the eye macro seems to be working quite nicely for me now:

macro_rules! guard_else {
    ($condition:expr, $body:block) => {
        let true = $condition else { $body };
    };
}

and to give a hideously contrived example:

for i in 1..=100 {
    guard_else!(i < 10, {
        // Arbitrary logic here...
        println!("DONE");
        break; // Compiler complains if this is missing (which is good!)
    });
    // ...
 }

This is very close to what I was originally envisaging and in very little code. Thank for the insight on using let-else in that way!

1 Like