Rust のエラーハンドリング
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
#[cxx::bridge]
mod ffi {
extern "Rust" {
fn fallible(depth: usize) -> Result<String>;
}
}
fn fallible(depth: usize) -> anyhow::Result<String> {
if depth == 0 {
return Err(anyhow::Error::msg("fallible1 requires depth > 0"));
}
Ok("Success!".into())
}
Resultを返す Rust 関数は、C++ 側では例外に変換されます。- スローされる例外は常に
rust::Error型であり、主にエラーメッセージ文字列を取得する手段を提供します。エラーメッセージは、エラー型のDisplayimpl から取得されます。 - Rust から C++ へ panic がアンワインドされると、プロセスは常に即座に終了します。