我正在努力处理一个应该返回 anf64
或 an的函数Error
。我尝试了很多组合,但没有成功。
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>