Anyhow for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Anyhow: What It Is and Why You Need It



Anyhow is one of the most popular libraries for error handling in the Rust ecosystem. It provides a simple, flexible, and convenient way to work with errors, especially in situations where you don't need strict error typing or when you're writing prototypes, scripts, CLI utilities, or applications where development speed is more important than perfect handling of every error variant.



In the Rust standard library, error handling is built around the Result<T, E> type, where E is the error type. However, in large projects, error types can vary: std::io::Error, serde_json::Error, reqwest::Error, etc. Combining them manually is a non-trivial task. Anyhow solves this problem by providing a universal anyhow::Error type that can store any error implementing the std::error::Error trait.



Key advantages of Anyhow:

  • Simplicity: Minimal boilerplate code. You don't need to create your own error types for every module.
  • Flexibility: Any error is automatically converted to anyhow::Error.
  • Context: You can add additional information to an error using the .context() method.
  • Convenient macros: bail! and ensure! allow you to quickly return errors.
  • Compatibility: Works with any library that returns Result.


Anyhow is not intended for libraries (where thiserror is better), but it is ideal for applications, binaries, and tests.



Installation



Anyhow is a Rust library, so installation is done via the cargo package manager. Add the following line to your Cargo.toml file:



[dependencies]anyhow = "1.0"


After that, run the cargo build command, and the library will be downloaded and compiled. If you are using Rust 2021 edition or newer, no additional configuration is required.



To use Anyhow in your code, simply import it:



use anyhow::{Result, anyhow, bail, Context};


Quick Start: First Example



Let's write a simple program that reads a file, parses it as JSON, and prints the result. If an error occurs at any stage, we simply return it upward.



use anyhow::Result;use std::fs;

fn main() -> Result<()> { // Read file contents let data = fs::read_to_string("config.json")?; // Parse as JSON (using serde_json) let config: serde_json::Value = serde_json::from_str(&data)?; println!("Configuration: {}", config); Ok(())}


Notice the ? — the error propagation operator. If read_to_string or from_str returns an error, it is automatically converted to anyhow::Error and the function terminates with that error. Simple and elegant.



Main Methods and Functions of Anyhow



Below are 10 key methods and functions of the Anyhow library with detailed descriptions and examples.



1. anyhow!() — Creating an Arbitrary Error

What it does: The anyhow! macro creates a new error of type anyhow::Error from a string or formatted message. It is analogous to format!, but for errors.

Parameters: Accepts a formatting string (like format!) or any type implementing Display.

Example:

use anyhow::{anyhow, Result};

fn validate_age(age: i32) -> Result<()> { if age < 0 {

Recommendations