In a current Rust project of mine I writing a compiler (not a classical one but still the best description of what I’m doing). One big part of it is a validator that takes the result of the Parser (an Abstract Syntax Tree) and does a lot of validation checks including type checking but does not transform the tree. The result here is Everything is Ok or a problem was found. Important here is that it does not exhaustively list all problems but aborts when it encounter the first one.
Currently this is done by using a Result type as return value that has a () type as its Ok part. As I do a lot of iterating over vectors and sometimes sets, I use plenty of for loops. Often these loops would be much more concise if I would use iterators and map, filter, all, for_each, … functions on them but I can not do this because I can not use the ? operator inside the closure to abort and return an Error Result, while in for loops I can do that.
I currently thinking about making a rewrite where I get rid of all the options and just panic when I detect an error. as it runs in a separate thread anyway this would be possible without a hassle and i could use a the iterator functions wherever I need.
It has to be noted that the input for this compiler is the output of another compiler, so i never expect an error during validation when the tool chain is used correctly but i still have to check it to protect against attacks that feed corrupted input to the compiler, which under no circumstances is allowed to return anything else than an error.
Readability and maintainability is an important part of this project and as such my question is which approach do you think is better suited and more readable/maintainable: Explicit Result as return or panic on error. I personally start to think that the panic approach could be a good fit here but panic comes with some stigma and thus I’m not really sure.