rust-result

Is it possible to convert Option<Result<T, E>> to a Result<Option<T>, E> without using match?

自作多情 提交于 2019-12-22 04:17:17
问题 My first thought is to map the Option , but I can't use try! from inside of the closure. The match statement looks unnecessary, but I can't figure out how to simplify it. fn example<T, E>(val: Option<Result<T, E>>) -> Result<Option<T>, E> { Ok(match val { Some(v) => Some(v?), None => None }) } 回答1: You can use Option::map_or(): val.map_or(Ok(None), |v| v.map(Some)) 回答2: In Rust 1.33, transpose() is stable, so you can just call it: fn main() { let x: Result<Option<i32>, ()> = Ok(Some(5)); let

Rust returns a result error from fn: mismatched types

孤人 提交于 2019-12-05 04:56:00
问题 I want this function to return an error result: fn get_result() -> Result<String, std::io::Error> { // Ok(String::from("foo")) <- works fine Result::Err(String::from("foo")) } Error Message error[E0308]: mismatched types --> src/main.rs:3:17 | 3 | Result::Err(String::from("foo")) | ^^^^^^^^^^^^^^^^^^^ expected struct `std::io::Error`, found struct `std::string::String` | = note: expected type `std::io::Error` found type `std::string::String` I'm confused how I can print out an error message

Rust returns a result error from fn: mismatched types

对着背影说爱祢 提交于 2019-12-03 20:19:38
I want this function to return an error result: fn get_result() -> Result<String, std::io::Error> { // Ok(String::from("foo")) <- works fine Result::Err(String::from("foo")) } Error Message error[E0308]: mismatched types --> src/main.rs:3:17 | 3 | Result::Err(String::from("foo")) | ^^^^^^^^^^^^^^^^^^^ expected struct `std::io::Error`, found struct `std::string::String` | = note: expected type `std::io::Error` found type `std::string::String` I'm confused how I can print out an error message when using the expected struct. The error message is quite clear. Your return type for get_result is

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

ぐ巨炮叔叔 提交于 2019-11-30 13:53:18
问题 This question already has answers here : How do I stop iteration and return an error when Iterator::map returns a Result::Err? (2 answers) Closed 7 months ago . I have code like this: let things = vec![/* ...*/]; // e.g. Vec<String> things .map(|thing| { let a = try!(do_stuff(thing)); Ok(other_stuff(a)) }) .filter(|thing_result| match *thing_result { Err(e) => true, Ok(a) => check(a), }) .map(|thing_result| { let a = try!(thing_result); // do stuff b }) .collect::<Result<Vec<_>, _>>() In

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

♀尐吖头ヾ 提交于 2019-11-30 08:31:28
This question already has an answer here: How do I stop iteration and return an error when Iterator::map returns a Result::Err? 2 answers I have code like this: let things = vec![/* ...*/]; // e.g. Vec<String> things .map(|thing| { let a = try!(do_stuff(thing)); Ok(other_stuff(a)) }) .filter(|thing_result| match *thing_result { Err(e) => true, Ok(a) => check(a), }) .map(|thing_result| { let a = try!(thing_result); // do stuff b }) .collect::<Result<Vec<_>, _>>() In terms of semantics, I want to stop processing after the first error. The above code works, but it feels quite cumbersome. Is there

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

↘锁芯ラ 提交于 2019-11-26 13:08:07
I have a function that returns a Result : fn find(id: &Id) -> Result<Item, ItemError> { // ... } Then another using it like this: let parent_items: Vec<Item> = parent_ids.iter() .map(|id| find(id).unwrap()) .collect(); How do I handle the case of failure inside any of the map iterations? I know I could use flat_map and in this case the error results would be ignored : let parent_items: Vec<Item> = parent_ids.iter() .flat_map(|id| find(id).into_iter()) .collect(); Result 's iterator has either 0 or 1 items depending on the success state, and flat_map will filter it out if it's 0. However, I don

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

不问归期 提交于 2019-11-26 02:09:24
问题 I have a function that returns a Result : fn find(id: &Id) -> Result<Item, ItemError> { // ... } Then another using it like this: let parent_items: Vec<Item> = parent_ids.iter() .map(|id| find(id).unwrap()) .collect(); How do I handle the case of failure inside any of the map iterations? I know I could use flat_map and in this case the error results would be ignored : let parent_items: Vec<Item> = parent_ids.iter() .flat_map(|id| find(id).into_iter()) .collect(); Result \'s iterator has