I have a library in which I want to convert a bunch of placeholder
One problem I have is that I have a parser object which implements the
Iterator trait, but it can return None if one of the nodes requests a controlled exit. When that happens the application can resume iteration on the parser (the progress is stored in the parser context; i.e. the parser is “consumed” when iterated over).
One solution is to return Option<Result<…>> from the
next(), but this feels a little clunky.
What is an acceptable/idiomatic way to handle iterators that can exit early either as a result of normal program flow or due to error, and where the application may need to act on the specific error?
One solution I’m looking at is is that the parser will store an Option which will be set by the iterator on error. The application can ask the parser if it exited due to an error or not, and if there was an error fetch the error context. I’m sure this will work, but I don’t know if this is frowned upon or if there’s already another (read: more idiomatic) way to do it.