{ rust }

  • 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.

  • Customized trait object in Rust

    | /

    I love the traits in Rust language. Rust uses one mechanism for both compile-time polymorphism and runtime polymorphism, which is elegant. However, the runtime polymorphism in Rust is not very flexible. Some traits cannot be used as a trait object, and it's impossible to specify whether a method is resolved dynamically or statically. This shortage is reflected in my recursive function library RecurFn, and I develop a workaround, calling it customized trait object.