How to resolve the algorithm Concurrent computing step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Concurrent computing step by step in the Rust programming language
Table of Contents
Problem Statement
Using either native language concurrency syntax or freely available libraries, write a program to display the strings "Enjoy" "Rosetta" "Code", one string per line, in random order. Concurrency syntax must use threads, tasks, co-routines, or whatever concurrency is called in your language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Concurrent computing step by step in the Rust programming language
Source code in the rust programming language
extern crate rand; // not needed for recent versions
use std::thread;
use rand::thread_rng;
use rand::distributions::{Range, IndependentSample};
fn main() {
let mut rng = thread_rng();
let rng_range = Range::new(0u32, 100);
for word in "Enjoy Rosetta Code".split_whitespace() {
let snooze_time = rng_range.ind_sample(&mut rng);
let local_word = word.to_owned();
std::thread::spawn(move || {
thread::sleep_ms(snooze_time);
println!("{}", local_word);
});
}
thread::sleep_ms(1000);
}
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Sexy primes step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the VBScript programming language
You may also check:How to resolve the algorithm Idoneal numbers step by step in the Action! programming language
You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the Raku programming language