调用 iter().sum() 时出错:特征绑定 `f64: std::iter::Sum<&std::vec::Vec<f64>>` 不满足

Error calling iter().sum(): the trait bound `f64: std::iter::Sum<&std::vec::Vec<f64>>` is not satisfied

我正在为一个应该 return f64Error 的函数而苦苦挣扎。我尝试了很多组合,但没有成功。

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[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>

Playground

你的主要问题在这里(简化了一点):

match all_payoffs {
    Ok(_v) => Ok(1.0 * (all_payoffs.iter().sum::<f64>())),
    Err(e) => { /*...*/ }
}

请注意,您的 all_payoffs 变量是 Result<Vec<f64>, Box<dyn Error + Send>> 类型。因此,当您执行 .iter() 时,您认为您得到的是 &f64 类型的向量值的迭代器,但实际上得到的是 Resultiterator for the ok-value ]!!

这在很多情况下都很方便,例如您可以使用 Iterator::flatten() 对所有值求和 if Ok() 但 return 0.0 if Err():

let x = all_payoffs.iter().flatten().sum::<f64>();

但是这里编译器认为您正在尝试对向量求和而无法完成,因此您会收到一条错误消息:

the trait bound f64: std::iter::Sum<&std::vec::Vec<f64>> is not satisfied

这基本上意味着:您不能对一堆 &Vec<f64> 求和并得到 f64 作为结果。

解决方案就在眼前,使用 _v:

Ok(v) => Ok(1.0 * v.iter().sum::<f64>()),

现在可以使用了!