Rust Result
Error handling in Rust
Rust Option も似たような感じのメソッドたくさんある
これ読むのが一番わかりやすい
std::result - Rust
Question mark ?
std::result - Rust
メソッドのドキュメント
Result in std::result - Rust
Rust エラー処理2020 - 電気ひつじ牧場
エラーのときになにかをしたい
unwrap_or_else が使える
code:on error
.unwrap_or_else(|err| eprintln!("{}", err);
map_error は何をする?
Err がきたら、処理をして、別の Err として返す
つまり Error ではない値を返したい場合には使えない
?メソッドはResult typeを返すメソッドないでだけよべる。
さまざまな Error があり得る場合は
https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#the--operator-can-be-used-in-functions-that-return-result
The Box<dyn Error> type is called a trait object, which we’ll talk about in the “Using Trait Objects that Allow for Values of Different Types” section in Chapter 17. For now, you can read Box<dyn Error> to mean “any kind of error.” Using ? in a main function with this return type is allowed.
code:errror.rs
use std::error::Error;
use std::fs::File;
fn main() -> Result<(), Box<dyn Error>> {
let f = File::open("hello.txt")?;
Ok(())
}
?の戻り値はOkの場合は結果のあたい。エラーならエラー。
RustでOption値やResult値を上手に扱う
map_error と or_else
Result in std::result - Rust
Result in std::result - Rust
かなり似ている?
map_error は 関数の戻り値を Err として返してくれる?
err しか返せない
or_else は、ok にすることもできる?