{ exception }

  • Simulate exception in async Rust

    | /

    WARNING: This may be a bad practice. The point of this article is just to share my idea.

    Exception is a popular feature in many programming languages. Unlike Rust where errors are considered as a return value of the Err case of Result, in these languages errors are thrown like panic. The caller may catch the errors and process them similar to catch_unwind in Rust. Different people may have different preferences on the error handling style, but panic-unwind is always considered a bad practice and should be avoided if possible. So using exception-like error handling in Rust is impossible.

    Well, it is possible with async Rust, but we need a little trick. The main idea here is to stop polling the future after an exception is thrown.