Результат не имеет метода "unwrap()"?
Какая странная ошибка:
use std::collections::BTreeMap;
struct MyStruct1;
struct Error;
fn get_res() -> Result<(MyStruct1, BTreeMap<String, String>), Error> {
Err(Error)
}
fn main() {
let res1 = get_res();
assert!(res1.is_ok());
assert_eq!("just for test", res1.unwrap()); //error
}
Ошибка:
error: no method named `unwrap` found for type `std::result::Result<(MyStruct1, std::collections::BTreeMap<std::string::String, std::string::String>), Error>` in the current scope
--> src/main.rs:13:38
|
13 | assert_eq!("just for test", res1.unwrap()); //error
| ^^^^^^
|
= note: the method `unwrap` exists but the following trait bounds were not satisfied: `Error : std::fmt::Debug`
Ответы
Ответ 1
Если вы прочитали документацию для Result::unwrap
, вы заметите, что она находится под небольшим сектором:
impl<T, E> Result<T, E>
where E: Debug
Это означает, что методы в этом разделе существуют только при условии выполнения заданных ограничений.
Единственная причина, по которой unwrap
не существует, заключается в том, что Error
не реализует Debug
.