What’s the most idiomatic way of working with an Iterator of Results? [duplicate]

There are lots of ways you could mean this. If you just want to panic, use .map(|x| x.unwrap()). If you want all results or a single error, collect into a Result<X<T>>: let results: Result<Vec<i32>, _> = result_i32_iter.collect(); If you want everything except the errors, use .filter_map(|x| x.ok()) or .flat_map(|x| x). If you want everything up … Read more

What is the idiomatic way to return an error from a function with no result if successful?

Use fn do_work() -> Result<(), WorkError>. Result<(), WorkError> means you want the work to be done, but it may fail. Option<WorkError> means you want to get an error, but it may be absent. You probably want the work to be done but not to get an error when you write do_work(), so Result<(), WorkError> is … Read more

How do I stop iteration and return an error when Iterator::map returns a Result::Err?

Result implements FromIterator, so you can move the Result outside and iterators will take care of the rest (including stopping iteration if an error is found). #[derive(Debug)] struct Item; type Id = String; fn find(id: &Id) -> Result<Item, String> { Err(format!(“Not found: {:?}”, id)) } fn main() { let s = |s: &str| s.to_string(); let … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)