【发布时间】:2019-04-30 23:03:29
【问题描述】:
我遇到了piston2d-graphics crate 的问题。当我尝试对从graphics::character::CharacterCache::character 方法获得的Result 使用expect() 方法时,结果证明我不能——因为它需要Result 的Error 类型来实现std::fmt::Debug trait:
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`
Error 这是CharacterCache trait 中的关联(嵌套)类型。我可以轻松地提交添加要求的 PR,然后使用简单的派生宏将其实现添加到所有其他 crate。这似乎是合理的,因为expect()和其他相关方法在Rust中一直使用,但我不确定。是 Rust 的方式,还是有理由不这样做?
我用它发生的例子来描述这个问题,但它与活塞无关,我的问题是关于 Rust 中的一般模式。所以标签rust-piston是无关的,请不要添加到问题中。
【问题讨论】:
-
在调用
expect的函数(以及可能调用第一个函数的其他函数)上添加where <C as graphics::character::CharacterCache>::Error : std::fmt::Debug也应该可以解决问题。 -
@FrancisGagné 但是除非我真正修复我正在使用的库,否则此功能将无法使用,而这个问题正是关于我是否应该这样做
-
哦,我假设有问题的错误类型已经实现
Debug。那没关系!
标签: rust