这是我之前问题的后续问题:Rust: Read and map lines from stdin and handling different error types
我创建了以下结构和函数来从 stdin 读取行并将它们解析为整数并且它有效:
use std::io::BufRead;
use std::{io, num, str};
#[derive(Debug)]
enum InputError {
IOError(io::Error),
ParseIntError(num::ParseIntError),
}
impl From<io::Error> for InputError {
fn from(e: io::Error) -> InputError {
return InputError::IOError(e);
}
}
impl From<num::ParseIntError> for InputError {
fn from(e: num::ParseIntError) -> InputError {
return InputError::ParseIntError(e);
}
}
pub fn get_integer_lines<T>() -> Result<Vec<T>, InputError>
where
T: str::FromStr,
{
let stdin = io::stdin();
let my_values: Result<Vec<_>, InputError> = stdin
.lock()
.lines()
.map(|line| -> Result<T, InputError> { Ok(line?.parse::<T>()?) })
.collect();
my_values
}
现在,我想我会替换 u32
类型参数 T 允许任何类型的数字类型。为此,我假设我需要将 T 限制为实现 FromStr 特性的类型,然后以某种方式实现 From 特性以允许从 FromStr::Err 转换为我的“InputError”。
按照我第一次得到的错误
error[E0277]: `?` couldn't convert the error to `InputError`
--> src/lib.rs:30:69
|
30 | .map(|line| -> Result<T, InputError> { Ok(line?.parse::<T>()?) })
| ^ the trait `std::convert::From<<T as std::str::FromStr>::Err>` is not implemented for `InputError`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: consider adding a `where InputError: std::convert::From<<T as std::str::FromStr>::Err>` bound
= note: required by `std::convert::From::from`
我试过这样的:
impl std::convert::From<<T as std::str::FromStr>::Err> for InputError {
fn from(e: <T as std::str::FromStr>::Err) -> InputError {
return InputError::ParseIntError(e)
}
}
但这反而会导致:
error[E0412]: cannot find type `T` in this scope
--> src/lib.rs:22:26
|
22 | impl std::convert::From<<T as std::str::FromStr>::Err> for InputError {
| ^ not found in this scope
所以基本上我想表达的是:
“我想为我的 From<T::Err>
实现特征 InputError
为每个 T
也实现 FromStr
。这甚至可能吗?如果可以,怎么做?
最佳答案
So basically what I want to express is something along the lines of: "I want to implement the trait
From<T::Err>
for myInputError
for everyT
which also implementsFromStr
. Is this even possible and if so, how?
这不是错误的意思。
性状 FromStr
有一个关联类型,Err
.错误是说此关联的错误类型无法转换为 InputError
.
首先,让我们通过去掉类型参数来简化:
fn get_integer_lines() -> Result<Vec<u32>, InputError> {
let stdin = io::stdin();
let my_values = stdin
.lock()
.lines()
.map(|line| Ok(line?.parse()?))
.collect();
my_values
}
这行得通!
关联的Err
输入 u32
的 FromStr
实现是 ParseIntError
并且您已正确实现 From<ParseIntError> for InputError
.
这对 T
不起作用的原因是因为T
的 FromStr::Err
类型可以是任何东西。通过使用类型参数,您告诉编译器您希望此函数适用于任何可能类型 T
, 但它只适用于 FromStr::Err
的类型可以转换成你的InputError
类型。
错误信息给了你一个提示:
= help: consider adding a `where InputError: std::convert::From<<T as std::str::FromStr>::Err>` bound
那么让我们这样做:
fn get_integer_lines<T>() -> Result<Vec<T>, InputError>
where
T: str::FromStr,
InputError: From<<T as str::FromStr>::Err>,
{
let stdin = io::stdin();
let my_values = stdin
.lock()
.lines()
.map(|line| Ok(line?.parse()?))
.collect();
my_values
}
这告诉编译器您希望该函数适用于所有可能的 T
前提是:
-
T
工具FromStr
并且, - 相关的
Err
从T
输入的FromStr
实现可以转换为InputError
关于Rust:为关联类型实现特征 "From"(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214210/