X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/bae9e47884fdc3fc1a81fb4844572a832fcfb2ce..98dafe0138b8bf6584b8d9e86a74a580bb034a26:/workspace/src/part12.rs?ds=inline
diff --git a/workspace/src/part12.rs b/workspace/src/part12.rs
index 84d47ec..4996ac1 100644
--- a/workspace/src/part12.rs
+++ b/workspace/src/part12.rs
@@ -1,5 +1,5 @@
-// Rust-101, Part 12: Concurrency (WIP)
-// =================
+// Rust-101, Part 12: Concurrency, Arc, Send
+// =========================================
use std::io::prelude::*;
use std::{io, fs, thread};
@@ -9,7 +9,6 @@ use std::sync::Arc;
// Before we come to the actual code, we define a data-structure `Options` to store all the information we need
// to complete the job: Which files to work on, which pattern to look for, and how to output.
-// Besides just printing all the matching lines, we will also offer to count them, or alternatively to sort them.
#[derive(Clone,Copy)]
pub enum OutputMode {
Print,
@@ -25,7 +24,7 @@ pub struct Options {
}
-// The first functions reads the files, and sends every line over the `out_channel`.
+// The first function reads the files, and sends every line over the `out_channel`.
fn read_files(options: Arc, out_channel: SyncSender) {
for file in options.files.iter() {
// First, we open the file, ignoring any errors.
@@ -43,11 +42,13 @@ fn read_files(options: Arc, out_channel: SyncSender) {
// The second function filters the lines it receives through `in_channel` with the pattern, and sends
// matches via `out_channel`.
-fn filter_lines(options: Arc, in_channel: Receiver, out_channel: SyncSender) {
+fn filter_lines(options: Arc,
+ in_channel: Receiver,
+ out_channel: SyncSender) {
// We can simply iterate over the channel, which will stop when the channel is closed.
for line in in_channel.iter() {
// `contains` works on lots of types of patterns, but in particular, we can use it to test whether
- // one string is contained in another.
+ // one string is contained in another. This is another example of Rust using traits as substitute for overloading.
if line.contains(&options.pattern) {
unimplemented!()
}
@@ -83,7 +84,7 @@ pub fn run(options: Options) {
// We move the `options` into an `Arc`, as that's what the thread workers expect.
let options = Arc::new(options);
- // Set up the channels. Use `sync_channel` with buffer-size of 16 to avoid needlessly filling RAM.
+ // This sets up the channels. We use a `sync_channel` with buffer-size of 16 to avoid needlessly filling RAM.
let (line_sender, line_receiver) = sync_channel(16);
let (filtered_sender, filtered_receiver) = sync_channel(16);
@@ -93,7 +94,9 @@ pub fn run(options: Options) {
// Same with the filter thread.
let options2 = options.clone();
- let handle2 = thread::spawn(move || filter_lines(options2, line_receiver, filtered_sender));
+ let handle2 = thread::spawn(move || {
+ filter_lines(options2, line_receiver, filtered_sender)
+ });
// And the output thread.
let options3 = options.clone();
@@ -108,14 +111,16 @@ pub fn run(options: Options) {
// Now we have all the pieces together for testing our rgrep with some hard-coded options.
pub fn main() {
let options = Options {
- files: vec!["src/part10.rs".to_string(), "src/part11.rs".to_string(), "src/part12.rs".to_string()],
+ files: vec!["src/part10.rs".to_string(),
+ "src/part11.rs".to_string(),
+ "src/part12.rs".to_string()],
pattern: "let".to_string(),
output_mode: Print
};
run(options);
}
-// **Exercise 12.1**: Change rgrep such that it prints now only the matching lines, but also the name of the file
+// **Exercise 12.1**: Change rgrep such that it prints not only the matching lines, but also the name of the file
// and the number of the line in the file. You will have to change the type of the channels from `String` to something
// that records this extra information.