Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can convert an `Option` into a `Result` and then use the `try!` macro, all in a single line: http://stackoverflow.com/a/37890739/46571


But Iterator's next() returns an Option, not a Result.


I don't see why you'd want to manipulate iterators by hand, unless you're implementing custom iterators. But, in any case:

    try!(iter.next().ok_or("end of sequence"))
EDIT: Sorry. I was just being dense.


Check out the code that goes along the "no try! for Option" comment in the OP: it is implementing a custom iterator.


Oh, my bad. I guess `try!` isn't a good enough replacement for properly treating `Option` and `Result` as monads.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: