---
title: How to Propagate Errors from main() in Rust
summary: Propagate errors from main() in Rust by returning Result<(), E> and using the ? operator or expect().
lang: en
slug: how-to-propagate-errors-from-main-in-rust
published: 2026-04-17T12:05:42+00:00
updated: 2026-04-25T22:00:13+00:00
canonical: https://www.rustfaq.org/en/how-to-propagate-errors-from-main-in-rust/
---

# How to Propagate Errors from main() in Rust



## When main needs to fail gracefully

You write a Rust script to process a configuration file. You run it, and the terminal spits out a wall of text: `thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }'`. You know the file is missing, but the error looks like a stack trace from a horror movie. The exit code is 101, which confuses shell scripts waiting for a success signal. You want a clean exit code and a message that actually helps the user.

Rust gives you a built-in way to handle this. The `main` function can return a `Result`. When it does, the runtime takes over error handling. It prints the error to stderr, sets the exit code to 1, and exits. You get a professional failure mode without writing boilerplate.

## The secret return type

By default, `main` returns `()`. If you return `()`, you must handle every error manually. You can print messages and call `std::process::exit`, but that skips cleanup and feels manual. The standard library provides a special rule for `main`: it can return `Result<(), E>` as long as the error type `E` implements `Debug`.

This rule turns your entire program into a single error-handling unit. Any function that returns a `Result` can use the `?` operator to propagate errors up to `main`. When `main` receives the error, it returns it. The runtime catches the return, formats the error using `Debug`, prints it, and exits with status 1. If `main` returns `Ok(())`, the runtime exits with status 0.

The runtime handles the formatting and exit code. You focus on the logic. This pattern is idiomatic for CLI tools, scripts, and any binary where the user needs to know why the program failed.

## Minimal example

Here is the simplest way to propagate errors from `main`. The function returns `Result<(), std::io::Error>`. The `?` operator handles the file open. If the file is missing, the error bubbles up and the runtime prints it.

```rust
use std::fs::File;
use std::io;

/// Opens a file and exits with an error if it fails.
/// Returns Result so the runtime can handle the error message and exit code.
fn main() -> Result<(), io::Error> {
    // Open the file. If this fails, the ? operator returns the error immediately.
    // The runtime prints the error and exits with code 1.
    let _file = File::open("config.txt")?;

    // If we reach here, the file opened successfully.
    println!("File opened.");
    Ok(())
}
```

Run this with a missing file. The output is clean: `Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }`. The exit code is 1. The user knows exactly what happened.

The runtime prints the error. You get the exit code. The user gets clarity.

## What the runtime does for you

When you return `Result<(), E>` from `main`, the compiler generates a wrapper function. This wrapper calls your `main`, checks the result, and handles the outcome. If the result is `Ok`, the wrapper returns 0 to the OS. If the result is `Err`, the wrapper prints the error using `Debug` formatting and returns 1.

The error type must implement `Debug`. This is a hard requirement. The runtime needs a way to display the error. If you use a standard type like `io::Error`, it already implements `Debug`. If you create a custom error type, you must derive `Debug`.

```rust
#[derive(Debug)]
struct MyError {
    message: String,
}
```

Without `Debug`, the compiler rejects the return type. You get E0277 (the trait `Debug` is not implemented for `MyError`). The compiler tells you exactly what is missing. Add the derive and the code compiles.

The runtime also respects the `Display` trait if you want custom formatting. Some runtimes or wrappers may use `Display` if available, but the guarantee is `Debug`. Rely on `Debug` for correctness. Implement `Display` if you want a prettier message for users.

## Handling multiple error types

Real programs often fail in different ways. You might read a file, parse JSON, and make a network request. Each operation returns a different error type. `io::Error`, `serde_json::Error`, `reqwest::Error`. `main` can only return one error type. If you return `io::Error`, you cannot propagate a JSON error directly.

You have two options. You can convert every error to `io::Error` using `map_err`. This works but creates artificial errors. A JSON parse error is not an IO error. Converting it loses information.

The better option for `main` is `Box<dyn Error>`. This is a trait object that erases the concrete error type. It allows `main` to return any error that implements the `Error` trait. The `?` operator automatically converts errors into `Box<dyn Error>` using the `From` trait.

```rust
use std::error::Error;
use std::fs;

/// Reads a file, parses a number, and prints it.
/// Returns Box<dyn Error> to handle multiple error types without conversion.
fn main() -> Result<(), Box<dyn Error>> {
    // Read the file. io::Error converts to Box<dyn Error> automatically.
    let content = fs::read_to_string("input.txt")?;

    // Parse the content. ParseIntError converts to Box<dyn Error> automatically.
    let number: i32 = content.trim().parse()?;

    println!("Number is {}", number);
    Ok(())
}
```

This code compiles and runs. If the file is missing, you get an IO error. If the content is not a number, you get a parse error. Both propagate cleanly. The runtime prints the error and exits.

Convention: Use `Box<dyn Error>` in `main` to avoid boilerplate. It is the standard escape hatch for binaries. Avoid it in library code. Libraries should return concrete error types so callers can match on specific errors. `Box<dyn Error>` hides the type and forces dynamic dispatch. It is convenient for `main` but harmful for APIs.

Don't fight the type system. Convert the error or widen the return type.

## Pitfalls and compiler errors

Returning `Result` from `main` is powerful, but the compiler enforces strict rules. Here are common mistakes and how to fix them.

If you try to use `?` in a `main` that returns `()`, the compiler rejects it. The `?` operator requires the function to return a `Result` or `Option`. You get E0277 (the `?` operator can only be applied to values that implement `Try`). Change the return type to `Result<(), E>` to fix this.

If you return the wrong error type, you get a mismatch. Suppose `main` returns `io::Error`, but you try to return a `String`. The compiler emits E0308 (mismatched types). You must convert the error or change the return type.

```rust
fn main() -> Result<(), std::io::Error> {
    // This fails to compile. String does not implement Into<io::Error>.
    // E0308: mismatched types.
    Err("Something went wrong".to_string())
}
```

Fix this by converting the error or using `Box<dyn Error>`.

If your custom error type lacks `Debug`, you get E0277. The runtime cannot print the error. Add `#[derive(Debug)]` to the struct.

```rust
#[derive(Debug)]
struct AppError {
    code: u32,
}

fn main() -> Result<(), AppError> {
    Err(AppError { code: 42 })
}
```

This compiles. The runtime prints `Error: AppError { code: 42 }`.

Another pitfall is ignoring the exit code. If you print an error message but return `Ok(())`, the program exits with code 0. Shell scripts and CI pipelines interpret this as success. Always return `Err` on failure. The runtime sets the exit code for you.

Treat the return value as the source of truth. If the program failed, return an error.

## Choosing your error strategy

Pick the strategy that matches the severity of the failure and the needs of your code.

Use `main() -> Result<(), E>` when you are writing a CLI tool or script where a clean exit code and error message matter. This is the standard pattern for utilities. The runtime handles printing and exit codes. You get reliability with zero boilerplate.

Use `main() -> Result<(), Box<dyn Error>>` when your program involves multiple error types and you want to avoid conversion boilerplate. This is the idiomatic choice for complex binaries. It allows `?` to work with any error type. Keep this pattern in `main` only.

Use `panic!` or `.expect()` when the program cannot continue and the error indicates a bug or an unrecoverable state. This is rare for IO errors but common for initialization failures that should never happen in production. Use `.expect()` with a clear message. Never use `.unwrap()` in production code.

Use a separate function returning `Result` when you want to test the logic or handle errors differently in different branches. `main` can call this function and propagate the result. This keeps `main` small and testable.

Use `std::process::exit` only when you must exit immediately without running cleanup code. This skips `Drop` implementations and can leak resources. Prefer returning `Result` from `main` for safe cleanup.

Pick the strategy that matches the severity of the failure.

## Where to go next

- [How to handle multiple error types](/en/how-to-handle-multiple-error-types/)
- [What is the ? operator](/en/what-is-the-operator/)
- [What is the difference between unwrap and expect](/en/what-is-the-difference-between-unwrap-and-expect/)