Need help understanding how `std::io::Error` is automatically getting converted to `Box<dyn std::error::Error>`

Hi there,

can someone please help me understand in the below code how std::io::error from fs::create_dir_all is automatically getting converted to Box<dyn std::error::Error> without calling .from() ?

type DynError = Box<dyn std::error::Error>;
fn dist() -> Result<(), DynError> {
    let _ = fs::remove_dir_all(&dist_dir());
    fs::create_dir_all(&dist_dir())?;

    dist_binary()?;
    dist_manpage()?;

    Ok(())
}

the whole code snippet can be found here

thanks in advance : )

The desugaring of the question mark operator includes calling From::from() on the error value.

1 Like

For more details you can check out the book: Recoverable Errors with Result - The Rust Programming Language

error values that have the ? operator called on them go through the from function, defined in the From trait in the standard library, which is used to convert values from one type into another. When the ? operator calls the from function, the error type received is converted into the error type defined in the return type of the current function.

1 Like

Thanks for quick response, now I feel silly not looking at ?’s body :sweat_smile:

Thanks for sharing : )

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.