LogoDuyệtSr. Data Engineer
HomeAboutPhotosInsightsCV

Footer

Logo

Resources

  • Rust Tiếng Việt
  • /archives
  • /series
  • /tags
  • Status

me@duyet.net

  • About
  • LinkedIn
  • Resume
  • Projects

© 2026 duyet.net | Sr. Data Engineer

Rust: Rayon - A data parallelism library for Rust

Chuỗi bài viết Rust Tiếng Việt là một trong những nội dung nằm trong sách Rust Tiếng Việt

rayon là thư viện data-parallelism cho Rust, gọn nhẹ và dễ dàng convert từ code tính toán tuần tự sang song song mà vẫn đảm bảo không lỗi data-race.

File: Cargo.toml

[dependencies]
rayon = "1.5"

Ví dụ:

use rayon::prelude::*;

fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- chỉ cần sử dụng `par_iter()` thay vì `iter()`!
         .map(|&i| i * i)
         .sum()
}

Parallel iterators sẽ phụ trách việc chia data thành nhiều tasks nhỏ như thế nào và sẽ đáp ứng linh hoạt để đạt maximum performance. Ngoài ra, Rayon cũng cung cấp 2 function join và scope để bạn có thể chủ động điều khiển việc parallel tasks.

Để tìm hiểu thêm về cách rayon hoạt động bạn có thể đọc thêm bài blog từ tác giả: https://smallcultfollowing.com/babysteps/blog/2015/12/18/rayon-data-parallelism-in-rust/

Demo & benchmark

https://rust-tieng-viet.github.io/crates/rayon.html#demo--bench

References

  • Rayon: data parallelism in Rust
  • https://www.youtube.com/watch?v=gof_OEv71Aw
  • https://github.com/rayon-rs/rayon
Aug 6, 2022·3 years ago
|Rust|
RustVietnameseRust Tiếng Việt
|Edit|
On this page
  • Demo & benchmark
  • References
On this page
  • Demo & benchmark
  • References