#error-string #error

simple-error

A simple error type backed by a string

20 releases

0.3.2 Oct 3, 2025
0.3.1 Jun 19, 2024
0.3.0 Feb 23, 2023
0.2.3 Jan 14, 2021
0.1.1 Mar 24, 2016

#923 in Rust patterns

Download history 35136/week @ 2025-09-24 35038/week @ 2025-10-01 33636/week @ 2025-10-08 33742/week @ 2025-10-15 33165/week @ 2025-10-22 35463/week @ 2025-10-29 32832/week @ 2025-11-05 34787/week @ 2025-11-12 32716/week @ 2025-11-19 19896/week @ 2025-11-26 33433/week @ 2025-12-03 32566/week @ 2025-12-10 27324/week @ 2025-12-17 6074/week @ 2025-12-24 12995/week @ 2025-12-31 34822/week @ 2026-01-07

88,542 downloads per month
Used in fewer than 95 crates

MIT/Apache

24KB
274 lines

simple-error

crates.io Documentation Build Status Coverage Status MSRV

simple-error is a Rust library that provides a simple Error type backed by a String. It is best used when all you care about the error is an error string.

Documentation

Usage

To use simple-error, first add this to your Cargo.toml:

[dependencies]
simple-error = "0.3"

Then import what you use (Rust 2018/2021):

use simple_error::{SimpleError, try_with};
// Add others as needed: require_with, ensure_with, bail, simple_error, map_err_with

Now you can use simple-error in different ways:

You can use it simply as a string error type:

fn do_foo() -> Result<(), SimpleError> {
    Err(SimpleError::new("cannot do foo"))
}

You can use it to replace all error types if you only care about a string description:

fn do_bar() -> Result<(), SimpleError> {
    Err(SimpleError::from(std::io::Error::new(std::io::ErrorKind::Other, "oh no")))
}

Or you can chain all the errors, and get a complete error description at the top level:

fn find_tv_remote() -> Result<(), SimpleError> {
    try_with!(std::fs::File::open("remotefile"), "failed to open remote file");
    Ok(())
}

fn turn_on_tv() -> Result<(), std::io::Error> {
    Ok(())
}

fn watch_tv() -> Result<(), SimpleError> {
    try_with!(find_tv_remote(), "tv remote not found");
    try_with!(turn_on_tv(), "cannot turn on tv");
    Ok(())
}

fn study() -> Result<(), SimpleError> {
    Ok(())
}

fn run() -> Result<(), SimpleError> {
    try_with!(study(), "cannot study");
    try_with!(watch_tv(), "cannot watch tv");
    Ok(())
}

fn main() {
    if let Err(e) = run() {
        println!("{}", e);
    }
}
// This prints out "cannot watch tv, tv remote not found, failed to open remote file, Text file busy" if the error is text file busy.

You can also ensure a condition holds and early-return a SimpleError if it does not:

use simple_error::{SimpleError, ensure_with};

fn check_config(is_valid: bool) -> Result<(), SimpleError> {
    ensure_with!(is_valid, "invalid config");
    Ok(())
}

Macros

  • try_with! — unwrap Result, else return SimpleError::with.
  • require_with! — unwrap Option, else return SimpleError::new.
  • ensure_with! — assert boolean, else return SimpleError::new.
  • bail! — return early with a SimpleError (supports format/expr).
  • simple_error! — construct a SimpleError (supports format/expr).
  • map_err_with! — map a Result’s error with extra context.

No runtime deps