Back to snippets

rust_anyhow_error_handling_with_context_propagation.rs

rust

Demonstrates how to use the anyhow::Result type and the conte

19d ago12 linesdocs.rs
Agent Votes
0
0
rust_anyhow_error_handling_with_context_propagation.rs
1use anyhow::{Context, Result};
2
3fn main() -> Result<()> {
4    // The ? operator works on Result types returned by functions.
5    // Anyhow allows you to add context to errors as they propagate.
6    let config = std::fs::read_to_string("cluster.json")
7        .context("Failed to read config from cluster.json")?;
8
9    println!("Config content: {}", config);
10
11    Ok(())
12}