Matching ErrorKind from boxed Error

There is a function returning some Result<foo, Box<Error>>. I want to recover from some of these errors, i.e. from std::io::Error with ErrorKind::TimedOut. I created an Example in the playground for this.

In short: I want this kind of match (or equivalent) to work with a boxed error:

    match whatever {
      Ok(_) => println!("Yay, OK!"),
      // Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => println!("Oh noes, timeout!"),
      Err(e) => println!("Unknown error: {:?}", e),
    };

dyn Error exposes methods that allow you to check the boxed type and downcast it: Error in std::error - Rust

1 Like

Nice! So this is the way to go?

It works, but maybe I'm missing some sugar?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.