Rustで並列処理と非同期処理を組み合わせる
サンプルコード
use tokio::runtime::Builder;
fn main() { // カスタムスレッドプールを使用してTokioランタイムを構築 let runtime = Builder::new_multi_thread() .worker_threads(4) .enable_all() .build() .unwrap();
runtime.block_on(async { let handles: Vec<_> = (0..10).map(|i| { tokio::spawn(async move { println!("Task {} is running", i); // 何か重い計算やI/O操作を行う }) }).collect();
for handle in handles { handle.await.unwrap(); } });}