该函数预计接受一个可以转换为字符串的命令,而且我也正在使用无论如何来处理错误。这是代码:
pub fn converter<T: TryInto<String>>(input: T) -> Result<String>
where
T::Error: Into<anyhow::Error>,
{
// error here
// the trait bound `<T as TryInto<std::string::String>>::Error: StdError` is not satisfied
// required because of the requirements on the impl of `From<<T as TryInto<std::string::String>>::Error>` for `anyhow::Error`
// required because of the requirements on the impl of `FromResidual<Result<Infallible, <T as TryInto<std::string::String>>::Error>>` for `Result<std::string::String, anyhow::Error>
let out_str = input.try_into()?;
Ok(out_str)
}
Run Code Online (Sandbox Code Playgroud)
T::Error我为as添加了类型限制T::Error: Into<anyhow::Error>,希望编译器能够进行错误处理,但显示T::Error无法转换为anyhow::Error.
类型限制引用自: https: //github.com/dtolnay/anyhow/issues/172,但我仍然不知道如何使用泛型TryInto。