How to Use map_err to Transform Errors in Rust

Use .map_err() to convert a Result's error type into a custom format before propagating it with the ? operator.

Use .map_err() on a Result to transform the error type while propagating success values. This method takes a closure that receives the original error and returns a new error type, allowing you to convert specific errors into a unified format like String.

use std::io::Read;

fn run() -> Result<(), String> {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf)
        .map_err(|e| format!("blah: {e}"))?;
    Ok(())
}

In the provided note.rs example, mdbook_preprocessor::parse_input returns a Result with a specific error type, which is immediately converted to a String using .map_err(|e| format!("blah: {e}")) before the ? operator propagates it.