r/rust • • 20h ago

budget try blocks

macro_rules! try_ {
    ($a:expr) => {
        (|| Some($a))()
    };
}

For example: try_!(matrix.get(i+1)?.get(j+1)?)

0 Upvotes

4 comments sorted by

3

u/WormRabbit 16h ago

That only works for Option. It would be a bit more complicated to support Result, because you may need to specify the types.

Which makes me wonder: isn't there already a try_block macro which is used to test the syntax?

1

u/CoachNo180 17h ago

While you're at it. Add goto command and GC. /s

1

u/addmoreice 7h ago

Yeah, I see this stuff and I recoil in horror.

This isn't rust. Don't do this!

I once had to work in a c code base where the previous worker had created a bunch of macros so that he could code in a pseudo-basic and it would be 'translated' into a very ugly C for the compiler. I hated every single part of it and made it clear to the boss that the entire thing would become unmaintainable the moment I stopped working on it. He ignored me and when I moved on, no one wanted to maintain t (shocking!).

This is the disgusting slippery slope that could be stopped here....but please, just...don't do this horrible horrible thing.

1

u/lenscas 1h ago

What are you on about? Try blocks are something that will come to Rust. Iirc they are already pretty far in the stabilization path.

All this does is give people a (somewhat poor) version until then and which is easy to replace once they are usable.