Questions tagged [rust-result]

32 questions
209
votes
4 answers

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

I have a function that returns a Result: fn find(id: &Id) -> Result { // ... } Then another using it like this: let parent_items: Vec = parent_ids.iter() .map(|id| find(id).unwrap()) .collect(); How do I handle…
Kai Sellgren
  • 27,954
  • 10
  • 75
  • 87
65
votes
2 answers

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

In Rust, I believe the idiomatic way to deal with recoverable errors is to use Result. For example this function clearly is idiomatic: fn do_work() -> Result {...} Of course, there are also functions that have a single, obvious,…
Others
  • 2,876
  • 2
  • 30
  • 52
53
votes
4 answers

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

I have code like this: let things = vec![/* ...*/]; // e.g. Vec things .map(|thing| { let a = try!(do_stuff(thing)); Ok(other_stuff(a)) }) .filter(|thing_result| match *thing_result { Err(e) => true, …
Tim McLean
  • 1,576
  • 3
  • 14
  • 20
23
votes
2 answers

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

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(val: Option>) -> Result, E> { …
GregoryComer
  • 740
  • 8
  • 18
13
votes
1 answer

What does dotenv().ok() do?

I am using the Diesel ORM wrapper with PostgreSQL. I was following the guide on their website which has the following code: pub fn establish_connection() -> PgConnection { dotenv().ok(); let database_url = env::var("DATABASE_URL") …
elementory
  • 133
  • 3
  • 7
11
votes
2 answers

Using and_then with different Result error types without map_err

I have some functions that will return a different error type when failing. First I have a builder, which contains this method: #[derive(Debug)] pub enum BuilderError { ElementMissing(&'static str), } pub fn spawn(self) -> Result
Sassa
  • 1,673
  • 2
  • 16
  • 30
8
votes
1 answer

Calling map on Iter of Results in Rust

I would like to write some code in a "functional programming" style. However, I start with an Iterator of Results and I only want to apply the function to the Ok items. Furthermore, I want to stop the iteration on the first error (however, I'd be…
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
8
votes
3 answers

Fold with closure that returns a Result

I'm using the regex crate to find some text with this regex: lazy_static! { static ref FIND_STEPS_RE: Regex = Regex::new(r"my regex").unwrap(); } I want to find all possible captures and iterate over…
Sassa
  • 1,673
  • 2
  • 16
  • 30
7
votes
1 answer

Cannot call a function that returns Result: found opaque type impl std::future::Future

I cannot return a result of a function from a Result. Every tutorial only shows how to use a Result, but not how to return a value from it. fn main(){ let mut a: Vec = Vec::new(); a = gottem(); println!("{}",…
Dude4
  • 163
  • 1
  • 8
5
votes
1 answer

What is the point of an Infallible Result, over just returning the Ok() branch?

The canonical example of a Warp rejection handler is async fn handle_rejection(err: Rejection) -> Result { But what's the advantage of a Result such that the err is Infallible and can never be reached? Why not just…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
5
votes
1 answer

Difference between match and unwrap_or when determining types

The following is a valid file I can compile with Rust 1.23.0: fn main() { let r = String::from("a"); let a = Some(&r); let b = match a { Some(name) => name, None => "", }; println!("{}", b); } Whilst the…
George
  • 3,521
  • 4
  • 30
  • 75
5
votes
2 answers

Rust returns a result error from fn: mismatched types

I want this function to return an error result: fn get_result() -> Result { // Ok(String::from("foo")) <- works fine Result::Err(String::from("foo")) } Error Message error[E0308]: mismatched types -->…
abdoe
  • 359
  • 1
  • 4
  • 11
4
votes
2 answers

Idiomatic way to collect all errors from an iterator

Let's say I have a attrs: Vec of some function attributes and a function fn map_attribute(attr: &Attribute) -> Result that maps the attributes to some code. I know that I could write something like…
msrd0
  • 7,816
  • 9
  • 47
  • 82
4
votes
1 answer

What does Result<()> without the error type mean?

I was looking at the std::env::current_dir documentation and this caught my attention: std::io::Result<()> I thought a Result should have a T and an E. How can you substitute them with ()?
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
3
votes
1 answer

Avoid multiple calls to `map_err` in a chain of Results

I am working with some code that uses the libgit2 bindings for Rust to perform operations on a Git repository. I have a section of code that transforms a "committish" reference (something that either is a commit or ultimately references a commit,…
larsks
  • 277,717
  • 41
  • 399
  • 399
1
2 3