+// Now, we can sort, e.g., an vector of numbers.
+fn sort_nums(data: &mut Vec<i32>) {
+ //@ Vectors support slicing, just like slices do. Here, `..` denotes the full range, which means we want to slice the entire vector.
+ //@ It is then passed to the `sort` function, which doesn't even know that it is working on data inside a vector.
+ sort(&mut data[..]);
+}
+
+// ## Arrays
+//@ An *array* in Rust is given by the type `[T; n]`, where `n` is some *fixed* number. So, `[f64; 10]` is an array of 10 floating-point
+//@ numbers, all one right next to the other in memory. Arrays are sized, and hence can be used like any other type. But we can also
+//@ borrow them as slices, e.g., to sort them.
+fn sort_array() {
+ let mut array_of_data: [f64; 5] = [1.0, 3.4, 12.7, -9.12, 0.1];
+ sort(&mut array_of_data);
+}
+
+// ## 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`](https://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 enable 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.
+// Remove the attribute of the `rgrep` module to enable compilation.
+#[cfg(feature = "disabled")]
+pub mod rgrep {
+ // Now that `docopt` is linked, we can first add it to the namespace with `extern crate` and then import shorter names with `use`.
+ // We also import some other pieces that we will need.
+ extern crate docopt;
+ use self::docopt::Docopt;
+ use part13::{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 {
+ // This parses `argv` and exit the program with an error message if it fails. The code is taken from the [`docopt` documentation](http://burntsushi.net/rustdoc/docopt/). <br/>
+ //@ 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);