Vec<Result> to Result<Vec>?

I found such code:

fn foo(s: &[i8]) -> Result<Vec<u8>, &'static str> {
        .map(|&x| if x < 0 { Err("aaa") } else { Ok(x as u8) })

can anybody explain, why it compiles?

After map we get Result<u8, &'static str>,
after collect: SomeCollection<Result<u8, &'static str>>,
but how SomeCollection of Result becomes Result<SomeCollection> ?

Result has a FromIterator impl, exactly so that this can work. In other words, your assertion that it's SomeCollection<Result> after collect() is wrong; the return type of foo() dictates that the return type of collect() must also be Result<Vec<_>, _>.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.