为什么我会得到这个错误以及如何修复它?这是我如何得到temp:let temp = self.value.clone().iter().map(|x| self.parse(x.to_string())).collect::<Vec<Result<T, String>>>();
它只是将一个字符串解析为T。
Ok(temp.iter().map(|x: &Result<T, String> | x?).collect::<Vec<T>>())
the `?` operator cannot be applied to type `&Result<T, String>`
|
= help: the trait `Try` is not implemented for `&Result<T, String>`
= help: the trait `Try` is implemented for `Result<T, E>
这是一个有效的修复,但上面的Map也应该有效。
let mut res = vec![];
for v in temp {
res.push(v?);
}
Ok(res)
1条答案
按热度按时间z3yyvxxp1#
出现此错误是因为不能将
?
与Result
s的引用一起使用。但是如果你有
Result
s,你所写的也不起作用,因为?
-operator early从最里面的函数(这里是闭包)返回,而你可以使用as_ref
将&Result<T, String>
转换为Result<&T, &String>
,并将collect
转换为Result
: