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.

3 Likes

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();
}
5 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

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