Rust Result
これ読むのが一番わかりやすい
Question mark ?
メソッドのドキュメント
エラーのときになにかをしたい
code:on error
.unwrap_or_else(|err| eprintln!("{}", err);
Err がきたら、処理をして、別の Err として返す
つまり Error ではない値を返したい場合には使えない
?メソッドはResult typeを返すメソッドないでだけよべる。
さまざまな Error があり得る場合は
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の場合は結果のあたい。エラーならエラー。
map_error と or_else
かなり似ている?
map_error は 関数の戻り値を Err として返してくれる?
err しか返せない
or_else は、ok にすることもできる?