For concrete type like u64, the following code compiles successfully.
use anyhow::{Context, Result};
fn parse_u64(num_str: &str) -> Result<u64> {
num_str
.parse::<u64>()
.with_context(|| format!("cannot parse {} as u64", num_str))
}
However the following code using generics does not compile.
use anyhow::{Context, Result};
use std::str::FromStr;
fn parse<F: FromStr>(num_str: &str) -> Result<F> {
num_str
.parse::<F>()
.with_context(|| format!("cannot parse {}", num_str))
}
I got the following error.
error[E0599]: no method named `with_context` found for enum `std::result::Result<F, <F as std::str::FromStr>::Err>` in the current scope
Is there a way to FromStr:Err with anyhow::Error?