我用的是这个代码:
// input is an arg here
let players: Option<Vec<Player>>;
if let Some(players_to_create) = input.players_to_create {
players = Some(
players_to_create
.iter()
.map(|o| Player::new(&o.id, &o.team_id, &o.name)?)
.collect(),
)
}
但我得到了这样一个错误:
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
|
61 | ... .map(|o| {
| --- this function should return `Result` or `Option` to accept `?`
...
67 | ... )?
| ^ cannot use the `?` operator in a closure that returns `Player`
|
= help: the trait `std::ops::FromResidual<std::result::Result<std::convert::Infallible, common::error::Error>>` is not implemented for `Player`
如果我删除?
,我会得到以下错误:
error[E0277]: a value of type `std::vec::Vec<Player>` cannot be built from an iterator over elements of type `std::result::Result<Player, common::error::Error>`
|
59 | / ... players_to_create
60 | | ... .iter()
61 | | ... .map(|o| {
62 | | ... Player::new(
... |
67 | | ... )
68 | | ... })
| |____________________________^ value of type `std::vec::Vec<Player>` cannot be built from `std::iter::Iterator<Item=std::result::Result<Player, common::error::Error>>`
69 | ... .collect(),
| ------- required by a bound introduced by this call
|
= help: the trait `std::iter::FromIterator<std::result::Result<Player, common::error::Error>>` is not implemented for `std::vec::Vec<Player>`
= help: the trait `std::iter::FromIterator<T>` is implemented for `std::vec::Vec<T>`
note: required by a bound in `std::iter::Iterator::collect`
|
1832 | fn collect<B: FromIterator<Self::Item>>(self) -> B
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::collect`
你能帮我理解一下吗?