Rust における Box を使ったエラー処理

Beginner

This tutorial is from open-source community. Access the source code

はじめに

この実験では、コードは Box 型を使用してエラーをラップすることで元のエラーを保持し、動的なエラー処理を可能にする方法を示しており、Std ライブラリの From トレイトは、Error トレイトを実装する任意の型をトレイトオブジェクト Box<Error> に変換するのに役立ちます。これには、カスタムエラー型を使用して Box を使ってエラーを変換および処理する例が含まれています。

注: 実験でファイル名が指定されていない場合、好きなファイル名を使用できます。たとえば、main.rs を使用して、rustc main.rs &&./main でコンパイルして実行することができます。

エラーのボックス化

元のエラーを保持しながらシンプルなコードを書く方法の 1 つは、それらを「ボックス化」することです。欠点は、潜在的なエラー型が実行時にのみ判明し、静的には決定されないことです。

標準ライブラリは、BoxFrom を介して Error トレイトを実装する任意の型からトレイトオブジェクト Box<Error> への変換を実装することで、エラーのボックス化を支援します。

use std::error;
use std::fmt;

// エイリアスを `Box<error::Error>` に変更します。
type Result<T> = std::result::Result<T, Box<dyn error::Error>>;

#[derive(Debug, Clone)]
struct EmptyVec;

impl fmt::Display for EmptyVec {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "invalid first item to double")
    }
}

impl error::Error for EmptyVec {}

fn double_first(vec: Vec<&str>) -> Result<i32> {
    vec.first()
     .ok_or_else(|| EmptyVec.into()) // Box に変換
     .and_then(|s| {
            s.parse::<i32>()
             .map_err(|e| e.into()) // Box に変換
             .map(|i| 2 * i)
        })
}

fn print(result: Result<i32>) {
    match result {
        Ok(n) => println!("The first doubled is {}", n),
        Err(e) => println!("Error: {}", e),
    }
}

fn main() {
    let numbers = vec!["42", "93", "18"];
    let empty = vec![];
    let strings = vec!["tofu", "93", "18"];

    print(double_first(numbers));
    print(double_first(empty));
    print(double_first(strings));
}

まとめ

おめでとうございます!あなたは「エラーのボックス化」の実験を完了しました。あなたのスキルを向上させるために、LabEx でさらに多くの実験を行って練習することができます。