Is it good practice to require associated Error types implement Debug trait?
我遇到了
1 2 3 4 5 6 7 8 | error[E0599]: no method named `expect` found for type `std::result::Result<graphics::character::Character<'_, <G as graphics::Graphics>::Texture>, <C as graphics::character::CharacterCache>::Error>` in the current scope --> src/some_file.rs:44:53 | 44 | let ch_glyph = glyphs.character(34, ch).expect("Couldn't load character"); | ^^^^^^ | = note: the method `expect` exists but the following trait bounds were not satisfied: `<C as graphics::character::CharacterCache>::Error : std::fmt::Debug` |
我以实例的形式描述了这个问题,但它与活塞没有任何特定关系,我的问题是关于Rust中的一般模式。 因此标记
Is it good practice to require associated Error types implement Debug trait?
是的,如果可能的话。 也许他们忘记了。
解决此问题的方法是使用
1 2 3 4 5 6 7 8 9 | struct Error; fn foo() -> Result<(), Error> { Ok(()) } fn main() { foo().expect("no error"); } |
1 2 3 4 5 6 7 8 | error[E0599]: no method named `expect` found for type `std::result::Result<(), Error>` in the current scope --> src/main.rs:8:11 | 8 | foo().expect("no error"); | ^^^^^^ | = note: the method `expect` exists but the following trait bounds were not satisfied: `Error : std::fmt::Debug` |
使用
1 2 3 4 5 6 7 8 9 | struct Error; fn foo() -> Result<(), Error> { Ok(()) } fn main() { foo().map_err(|_| ()).expect("no error"); } |