development:software:coding-best-practices-rust

There are several ways to implement the same approach in Rust. Here we collect implementation details for different usecases to provide useful examples.

use anyhow::{anyhow, Result};

fn function_1_1() -> Result<()> {
...
    let opt = Some(1);
    let value = opt.ok_or(anyhow!("Error message"))?;
...
}

use anyhow::{anyhow, Result};
use rust_decimal::Decimal;

fn function_1_2() -> Result<()> {
...
    let opt = Some(1.2);
    let value: Decimal = match opt {
        Some(v) => v.try_into()?,
        None => anyhow::bail!("Error message"),
    };

use anyhow::{anyhow, Result};
use log;

fn function_returns_result() -> Result<()> {..}

if let Err(e) = function_returns_result() {
    log::debug!("Error: {}", e)
};

  • development/software/coding-best-practices-rust.txt
  • Last modified: 2024/08/08 14:25
  • by Jens Krause