Make Box<dyn Error> from String?

Hi, is there a function that will construct a Box<dyn Error> from a passed in String? This would be very handy for quickly making errors.

There is a From (and hence Into) implementation, so you can simply call .into() on the String (playground):

use std::error::Error;

fn main() {
    let _: Box<dyn Error> = String::from("test").into();
}
2 Likes

Something could be helpful: Basic Error Handling question: generic error type in return

I recently asked a question which is kind of related and it might be interesting for you to read the answers:

1 Like