The example is from here and mentioned in the Tokio tutorial:
async fn parallel_sum(nums: Vec<i32>) -> i32 {
let (send, recv) = tokio::sync::oneshot::channel();
// Spawn a task on rayon.
rayon::spawn(move || {
// Perform an expensive computation.
let mut sum = 0;
for num in nums {
sum += num;
}
// Send the result back to Tokio.
let _ = send.send(sum);
});
// Wait for the rayon task.
recv.await.expect("Panic in rayon::spawn")
}
#[tokio::main]
async fn main() { ▶︎ Run |Debug
let nums = vec![1; 1024 * 1024];
println!("{}", parallel_sum(nums).await);
}
This yields 1048576 on my box which is wrong IMHO…
karl@rantanplan:~/src/rust/playground/musashi$ cargo run --bin rayon
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
Running `/home/peter/src/rust/playground/target/debug/rayon`
1048576
karl@rantanplan:~/src/rust/playground/musashi$ perl -E 'say 1024*1024'
1048576
How come? Have I forgotten or overlooked something?
By the way, it is terribly slow.
Ooops, i have overseen the ;
- my bad! Sorry!!!