Hello folks,
I have an unconventional question in terms of flow control, and would appreciate any advice that could point me to the right direction.
In short, I like to have something like try catch
or throw recover
in other languages, and wonder what could be a good way to do it?
But, before you go "that's not what you're supposed to do in Rust", I'm very well aware that's the case, and I'm not a newbie.
It's for a library that there's a legitimate need to exit the running program early, and the caller need to be able to resume based on what the context of the early exit is.
Here's a sample code to help with some reference.
let Some(func) = self.funcs.get(&fn_id) else {
return Err("foobar");
}
let input = Input::new()
// This need to be able to catch specific type of errors that exit the function call early.
let res = (func.func)(&input);
The func.func
here is an anonymous function provided by the users of the library.
Inside that, users could be using other features of the library, that could throw intentionally to exit the function execution.
So Result
here is pretty much useless, because that can't stop the program as I need it to.
I've checked catch_unwind
but there are a lot of do no recommend, and also state in certain cases, it might not even unwind (e.g. if someone set panic = abort
in Cargo, then it's pretty much pointless).
After doing some more searching, it doesn't seem like there are other options available besides that, or I might just be looking at the wrong place entirely.
So any advice would be appreciated.