I've got the following piece of code (see playground):
use futures::{stream, Future, Stream}; // 0.1.25
use std::num::ParseIntError;
fn into_many(i: i32) -> impl Stream<Item = i32, Error = ParseIntError> {
stream::iter_ok(0..i)
}
fn convert_to_string(number: i32) -> Result<String, ParseIntError> {
Ok(number.to_string())
}
fn main() {
println!("start:");
let vec = into_many(10)
.map(|number| convert_to_string(number))
.collect()
.wait()
.unwrap();
println!("vec={:#?}", vec);
println!("finish:");
}
It outputs the following (i.e., Vec<Result<i32, ParseIntError>>
):
start:
vec=[
Ok(
"0"
),
Ok(
"1"
),
Ok(
"2"
), ...
Is there any way to make it output a Vec<i32>
and if any error happens than immediately stop execution and return from the function (e.g., like this example)?
Note: I do want to use use futures::Stream; // 0.1.25
even if it doesn't make sense for this particular example.
into_iter()
without implementing it. – DinnageTry
not implemented for String)? With a quick glance, it looks like you need to stop using?
inside the map function and see if collect can do this for you (the linked docs has an example which is similar to what you want). You can't just copy-paste other solutions; you'll need to understand the types you are working with and develop a solution that fits. – SidoniaVec<Result<>>
I think iscollect()
does this forIterator
but can't do it forStream
and I can't dointo_iter()
. – Dinnage?
I receive ``` = note: expected typestd::vec::Vec<i64>
found type `std::vec::Vec<std::result::Result<i64, errors::CustomError>>``` – Dinnage