動態錯誤型別
Sometimes we want to allow any type of error to be returned without writing our own enum covering all the different possibilities. The std::error::Error
trait makes it easy to create a trait object that can contain any error.
Speaker Notes
This slide should take about 5 minutes.
read_count
函式可以傳回 std::io::Error
(透過檔案作業) 或 std::num::ParseIntError
(透過 String::parse
)。
Boxing errors saves on code, but gives up the ability to cleanly handle different error cases differently in the program. As such it's generally not a good idea to use Box<dyn Error>
in the public API of a library, but it can be a good option in a program where you just want to display the error message somewhere.
定義自訂錯誤型別時,請務必實作 std::error::Error
特徵,這樣才能裝箱。不過,如果您需要支援 no_std
屬性,請留意 std::error::Error
特徵目前僅與每夜版中的 no_std
相容。