-// ## External Dependencies
-//@ This leaves us with just one more piece to complete rgrep: Taking arguments from the command-line. We could now directly work on
-//@ [`std::env::args`](http://doc.rust-lang.org/stable/std/env/fn.args.html) to gain access to those arguments, and this would become
-//@ a pretty boring lesson in string manipulation. Instead, I want to use this opportunity to show how easy it is to benefit from
-//@ other people's work in your program.
-//@
-//@ For sure, we are not the first to equip a Rust program with support for command-line arguments. Someone must have written a library
-//@ for the job, right? Indeed, someone has. Rust has a central repository of published libraries, called [crates.io](https://crates.io/).
-//@ It's a bit like [PyPI](https://pypi.python.org/pypi) or the [Ruby Gems](https://rubygems.org/): Everybody can upload their code,
-//@ and there's tooling for importing that code into your project. This tooling is provided by `cargo`, the tool we are already using to
-//@ build this tutorial. (`cargo` also has support for *publishing* your crate on crates.io, I refer you to [the documentation](http://doc.crates.io/crates-io.html) for more details.)
-//@ In this case, we are going to use the [`docopt` crate](https://crates.io/crates/docopt), which creates a parser for command-line
-//@ arguments based on the usage string. External dependencies are declared in the `Cargo.toml` file.
-
-//@ I already prepared that file, but the declaration of the dependency is still commented out. So please open `Cargo.toml` of your workspace
-//@ now, and enabled the two commented-out lines. Then do `cargo build`. Cargo will now download the crate from crates.io, compile it,
-//@ and link it to your program. In the future, you can do `cargo update` to make it download new versions of crates you depend on.
-//@ Note that crates.io is only the default location for dependencies, you can also give it the URL of a git repository or some local
-//@ path. All of this is explained in the [Cargo Guide](http://doc.crates.io/guide.html).
-
-// I disabled the following module (using a rather bad hack), because it only compiles if `docopt` is linked. However, before enabling it,
-// you still have get the external library into the global namespace. This is done with `extern crate docopt`, and that statement *has* to be
-// in `main.rs`. So please go there, and enable this commented-out line. Then remove the attribute of the `rgrep` module.
-#[cfg(feature = "disabled")]
-pub mod rgrep {
- // Now that `docopt` is linked and declared in `main.rs`, we can import it with `use`. We also import some other pieces that we will need.
- use docopt::Docopt;
- use part12::{run, Options, OutputMode};
- use std::process;
-
- // The `USAGE` string documents how the program is to be called. It's written in a format that `docopt` can parse.
- static USAGE: &'static str = "
-Usage: rgrep [-c] [-s] <pattern> <file>...
-
-Options:
- -c, --count Count number of matching lines (rather than printing them).
- -s, --sort Sort the lines before printing.
-";
-
- // This function extracts the rgrep options from the command-line arguments.
- fn get_options() -> Options {
- // Parse `argv` and exit the program with an error message if it fails. This is taken from the [`docopt` documentation](http://burntsushi.net/rustdoc/docopt/).
- //@ The function `and_then` takes a closure from `T` to `Result<U, E>`, and uses it to transform a `Result<T, E>` to a
- //@ `Result<U, E>`. This way, we can chain computations that only happen if the previous one succeeded (and the error
- //@ type has to stay the same). In case you know about monads, this style of programming will be familiar to you.
- //@ There's a similar function for `Option`. `unwrap_or_else` is a bit like `unwrap`, but rather than panicking in
- //@ case of an `Err`, it calls the closure.
- let args = Docopt::new(USAGE).and_then(|d| d.parse()).unwrap_or_else(|e| e.exit());
- // Now we can get all the values out.
- let count = args.get_bool("-c");
- let sort = args.get_bool("-s");
- let pattern = args.get_str("<pattern>");
- let files = args.get_vec("<file>");
- if count && sort {
- println!("Setting both '-c' and '-s' at the same time does not make any sense.");
- process::exit(1);
+// 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<Options>,
+ in_channel: Receiver<String>,
+ out_channel: SyncSender<String>) {
+ // 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. This is another example of Rust using traits as substitute for overloading.
+ if line.contains(&options.pattern) {
+ out_channel.send(line).unwrap(); /*@*/