Читаем Rust by Example полностью

// iterate over the characters of our segment..

.chars()

// .. convert text-characters to their number value..

.map(|c| c.to_digit(10).expect("should be a digit"))

// .. and sum the resulting iterator of numbers

.sum();

// println! locks stdout, so no text-interleaving occurs

println!("processed segment {}, result={}", i, result);

// "return" not needed, because Rust is an "expression language", the

// last evaluated expression in each block is automatically its value.

result

}));

}

/*************************************************************************

* "Reduce" phase

*

* Collect our intermediate results, and combine them into a final result

************************************************************************/

// combine each thread's intermediate results into a single final sum.

//

// we use the "turbofish" ::<> to provide sum() with a type hint.

//

// TODO: try without the turbofish, by instead explicitly

// specifying the type of final_result

let final_result = children.into_iter().map(|c| c.join().unwrap()).sum::();

println!("Final sum result: {}", final_result);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="assignments"><strong><a l:href="#assignments">Assignments</a></strong></p>

It is not wise to let our number of threads depend on user inputted data. What if the user decides to insert a lot of spaces? Do we really want to spawn 2,000 threads? Modify the program so that the data is always chunked into a limited number of chunks, defined by a static constant at the beginning of the program.

<p id="see_also_69"><strong><a l:href="#see_also_69">See also:</a></strong></p>

   • Threads

   • vectors and iterators

   • closures, move semantics and move closures

   • destructuring assignments

   • turbofish notation to help type inference

   • unwrap vs. expect

   • enumerate

<p id="channels"><strong><a l:href="#channels">Channels</a></strong></p>

Rust provides asynchronous channels for communication between threads. Channels allow a unidirectional flow of information between two end-points: the Sender and the Receiver.

use std::sync::mpsc::{Sender, Receiver};

use std::sync::mpsc;

use std::thread;

static NTHREADS: i32 = 3;

fn main() {

// Channels have two endpoints: the `Sender` and the `Receiver`,

// where `T` is the type of the message to be transferred

// (type annotation is superfluous)

let (tx, rx): (Sender, Receiver) = mpsc::channel();

let mut children = Vec::new();

for id in 0..NTHREADS {

// The sender endpoint can be copied

let thread_tx = tx.clone();

// Each thread will send its id via the channel

Перейти на страницу:

Похожие книги