# Having trouble with function return

**URL:** <https://users.rust-lang.org/t/having-trouble-with-function-return/42633>\
**Category:** help\
**Created:** [May 14, 2020, 6:56pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633 "2020-05-14T18:56:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![suhasghorp](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suhasghorp](https://users.rust-lang.org/u/suhasghorp)\
**Post date:** [May 14, 2020, 6:56pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/1 "2020-05-14T18:56:13Z")

</div>

I am struggling with a function that is supposed to return either an `f64` or an `Error`. I have tried many combinations, but no success. Thanks in advance. Also posted on Stackoverflow.

```
fn_rayon_parallel(&self) -> Result<f64, Box<dyn Error + Send + Sync>> {

    let all_payoffs = (0..10000).into_par_iter().map( 
        |_| { //things could go very wrong here        
                match self.get_one_payoff() {
                    Ok(payoff) => {Ok(payoff)},
                    Err(e) => Err(e)
                }
            }
    ).collect::<Result<Vec<_>, _>>();
    
    //this is the part I am struggling with, does not compile
    //I need to return either f64 or Error

    match all_payoffs {
        Ok(_v) => Ok(2.34 * (1.0/10000.0) * (all_payoffs.iter().sum::<f64>())),
        Err(e) => Err(From::from("There was an error in calculating Discounted Payoffs."))
    }

    
}

```

Error:

```
error[E0277]: the trait bound `f64: std::iter::Sum<&std::vec::Vec<f64>>` is not satisfied
  --> src/main.rs:81:69
   |
81 | Ok(_v) => Ok(2.34 * (1.0/10000.0) * (all_payoffs.iter().sum::<f64>())),
   | ^^^ the trait `std::iter::Sum<&std::vec::Vec<f64>>` is not implemented for `f64`
   |
   = help: the following implementations were found:
             <f64 as std::iter::Sum<&'a f64>>
             <f64 as std::iter::Sum>

```

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=28d325ae926d36dcd50f526f7d2d2ffd)**
>
> A browser interface to the Rust compiler to experiment with the language

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [May 14, 2020, 7:03pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/2 "2020-05-14T19:03:41Z")

</div>

You should reference the thing you called `_v` instead of `all_payoffs`. You are currently calling [`Result::iter`](https://doc.rust-lang.org/stable/std/result/enum.Result.html#method.iter), which is not what you wanted, and this is why the item type in your iterator is `Vec<f64>`.

Note that we typically don't throw away the error information like that. It's standard to either return `e` directly or return something that includes the source error information.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [May 14, 2020, 7:38pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/3 "2020-05-14T19:38:27Z")

</div>

FWIW, `Result` also implements `Sum`, and you can use that in `ParallelIterator::sum`. So unless you really need that vector intermediate, you could do it like this:

```rust
    fn fn_rayon_parallel(&self) -> Result<f64, Box<dyn Error + Send + Sync>> {
        let sum_result = (0..10000)
            .into_par_iter()
            .map(|_| {
                //things could go very wrong here
                match self.get_one_payoff() {
                    Ok(payoff) => Ok(payoff),
                    Err(e) => Err(e),
                }
            })
            .sum::<Result<f64, _>>();

        match sum_result {
            Ok(x) => Ok(2.34 * (1.0 / 10000.0) * x),
            Err(e) => Err(From::from(
                "There was an error in calculating Discounted Payoffs.",
            )),
        }
    }

```

---

<div class="post-metadata">

**Author:** ![suhasghorp](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suhasghorp](https://users.rust-lang.org/u/suhasghorp)\
**Post date:** [May 14, 2020, 9:08pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/4 "2020-05-14T21:08:38Z")

</div>

Awesome tip! Thanks

---

<div class="post-metadata">

**Author:** ![suhasghorp](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suhasghorp](https://users.rust-lang.org/u/suhasghorp)\
**Post date:** [May 14, 2020, 9:09pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/5 "2020-05-14T21:09:48Z")

</div>

Thanks! Any idea how do I return the error directly back to the caller?

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [May 15, 2020, 5:06am UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/6 "2020-05-15T05:06:47Z")

</div>

You can either return `Err(e.into())` or use the question mark operator to replace the match with

```rust
Ok(2.34 * (1.0/10000.0) * (all_payoffs?.iter().sum::<f64>()))

```

---

<div class="post-metadata">

**Author:** ![suhasghorp](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@suhasghorp](https://users.rust-lang.org/u/suhasghorp)\
**Post date:** [May 15, 2020, 2:45pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/7 "2020-05-15T14:45:05Z")

</div>

Got it. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 13, 2020, 2:45pm UTC](https://users.rust-lang.org/t/having-trouble-with-function-return/42633/8 "2020-08-13T14:45:25Z")

</div>

Closed on August 13, 2020, 2:45pm UTC

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