0Pricing
Learn Rust Coding · Lesson

Error Conversion

From implementations.

Error Conversion is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Convert Errors?

Different layers of code produce different error types. To propagate them through one function, you need to convert lower-level errors into a unified type. Rust does this through the From trait.

The From Trait

From<T> defines how to build one type from another. Implementing it for your error type lets you convert source errors into it.

trait From<T> {
    fn from(value: T) -> Self;
}

How ? Uses From

When you write expr? and the error type differs from the function's return error, the operator calls From::from to convert it. This is the magic behind seamless propagation.

A Unified Error Type

Define an enum to hold each kind of error your function may emit.

#[derive(Debug)]
enum AppError {
    Io(std::io::Error),
    Parse(std::num::ParseIntError),
}

Implementing From

Write a From impl for each source error so it converts into the matching variant.

impl From<std::io::Error> for AppError {
    fn from(e: std::io::Error) -> Self {
        AppError::Io(e)
    }
}

impl From<std::num::ParseIntError> for AppError {
    fn from(e: std::num::ParseIntError) -> Self {
        AppError::Parse(e)
    }
}

Propagating Automatically

With those impls in place, ? converts each error into AppError for free.

fn read_count(path: &str) -> Result<i32, AppError> {
    let text = std::fs::read_to_string(path)?; // io::Error -> AppError
    let n: i32 = text.trim().parse()?;          // ParseIntError -> AppError
    Ok(n)
}

Into Is the Mirror

Implementing From<A> for B automatically gives you A: Into<B>. Prefer implementing From; you get into() for free.

fn make() -> AppError {
    let e: std::io::Error = std::io::Error::other("boom");
    e.into() // uses From<io::Error> for AppError
}

Boxing Trait Objects

For quick conversions, Box<dyn Error> accepts any error type, since the standard library provides blanket From impls into it.

use std::error::Error;

fn run() -> Result<(), Box<dyn Error>> {
    let n: i32 = "42".parse()?; // ParseIntError -> Box<dyn Error>
    println!("{n}");
    Ok(())
}

Crates That Generate From

Writing each From impl by hand is tedious. thiserror's #[from] attribute generates them, and anyhow converts everything into one type automatically.

Choosing an Approach

Summary of conversion strategies:

  • Manual From impls — full control, more code
  • thiserror #[from] — typed errors, generated impls
  • Box<dyn Error> or anyhow — minimal ceremony

Adding Context During Conversion

A From impl can do more than wrap — it can enrich the error. Here the conversion attaches extra context so the final error is more informative.

impl From<std::num::ParseIntError> for AppError {
    fn from(e: std::num::ParseIntError) -> Self {
        // could log or annotate here before wrapping
        AppError::Parse(e)
    }
}

Quick Check

Which trait does the ? operator use to convert one error type into another?

Recap

You learned error conversion:

  • ? converts errors via the From trait
  • Implement From<Source> for your error enum
  • From gives you Into and into() for free
  • Box<dyn Error> accepts any error
  • thiserror and anyhow automate the boilerplate

Frequently asked questions

Is the “Error Conversion” lesson free?

Yes — the full text of “Error Conversion” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Error Conversion”?

From implementations. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Error Conversion” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Result and the ? Operator
  2. thiserror
  3. anyhow
  4. Error Conversion
← Back to Learn Rust Coding