3 ms·
> let res: Vec<_> = iterator.iter().map(|x| x.foo()?).collect(); This make sense to me? Why shouldn't it return early from the lambda in `map`? Had it return t
by kajaktum 4y ago
> let res: Vec<_> = iterator.iter().map(|x| x.foo()?).collect();
This make sense to me? Why shouldn't it return early from the lambda in `map`? Had it return the "highest level function" in the scope then that would be bug prone imo. It is already possible to do fallible iteration:
> let res: Vec<_> = iterator.iter().map(|x| x.foo()).collect::<Vec<Result<_,_>>>()?;
However, I would like to mention that you should not try to force the syntax. This mantra is very misleading imo
> There should be one -- and preferably only one -- obvious way to do it.
Had Rust gone with your suggestion, imagine writing the equivalent of this?
```
fn foo() -> Result<String, String> {
Ok("hello".to_string())
}
fn main() {
println!(
"{:?}",
(0..2)
.map(|_| {
foo()?;
foo()?;
foo()?;
foo()?;
foo()?;
Ok::<String,String>("world".to_string())
})
.collect::<Vec<Result<_, _>>>()
);
}
```
How a "hacker news" website not supporting code snippet is beyond me.
- estebank 4y ago> How a "hacker news" website not supporting code snippet is beyond me. Indent your code by two spaces.