'how to convert anyhow error to std error?
I am new to anyhow and I would like to know if there is a way to convert anyhow error to std error
code
fn m() {
let a: anyhow::Result<String> = Ok("".into());
n(a);
}
fn n(r: std::result::Result<String, impl std::error::Error>) {
println!("{:?}", r);
}
error msg
error[E0277]: the trait bound `anyhow::Error: std::error::Error` is not satisfied
--> src\main.rs:82:7
|
82 | n(a);
| - ^ the trait `std::error::Error` is not implemented for `anyhow::Error`
| |
| required by a bound introduced by this call
Solution 1:[1]
match a {
Ok(v) => n(Ok::<_, &dyn std::error::Error>(v)),
Err(e) => n(Err::<_, &dyn std::error::Error>(e.as_ref())),
}
// Or
match a {
Ok(v) => n(Ok::<_, &dyn std::error::Error>(v)),
Err(e) => n(Err::<_, &dyn std::error::Error>(&*e)),
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chayim Friedman |
