+// With the operations of the three threads defined, we can now implement a function that performs grepping according
+// to some given options.
+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);
+
+ // 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);
+
+ // Spawn the read thread: `thread::spawn` takes a closure that is run in a new thread.
+ let options1 = options.clone();
+ let handle1 = thread::spawn(move || read_files(options1, line_sender));
+
+ // Same with the filter thread.
+ let options2 = options.clone();
+ let handle2 = thread::spawn(move || {
+ filter_lines(options2, line_receiver, filtered_sender)
+ });
+
+ // And the output thread.
+ let options3 = options.clone();
+ let handle3 = thread::spawn(move || output_lines(options3, filtered_receiver));
+
+ // Finally, wait until all three threads did their job.
+ handle1.join().unwrap();
+ handle2.join().unwrap();
+ handle3.join().unwrap();
+}
+
+// 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()],
+ pattern: "let".to_string(),
+ output_mode: Print
+ };
+ run(options);
+}
+
+// **Exercise 13.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.
+
+