1 // Rust-101, Part 13: Slices, Arrays, External Dependencies
6 pub fn sort<T: PartialOrd>(data: &mut [T]) {
7 if data.len() < 2 { return; }
9 // We decide that the element at 0 is our pivot, and then we move our cursors through the rest of the slice,
10 // making sure that everything on the left is no larger than the pivot, and everything on the right is no smaller.
12 let mut rpos = data.len();
13 /* Invariant: pivot is data[0]; everything with index (0,lpos) is <= pivot; [rpos,len) is >= pivot; lpos < rpos */
15 // **Exercise 13.1**: Complete this Quicksort loop. You can use `swap` on slices to swap two elements.
19 // Once our cursors met, we need to put the pivot in the right place.
22 // Finally, we split our slice to sort the two halves. The nice part about slices is that splitting them is cheap:
23 let (part1, part2) = data.split_at_mut(lpos);
27 // **Exercise 13.2*: Since `String` implements `PartialEq`, you can now change the function `output_lines` in the previous part
28 // to call the sort function above. If you did exercise 12.1, you will have slightly more work. Make sure you sort by the matched line
29 // only, not by filename or line number!
31 // Now, we can sort, e.g., an vector of numbers.
32 fn sort_nums(data: &mut Vec<i32>) {
38 let mut data: [f64; 5] = [1.0, 3.4, 12.7, -9.12, 0.1];
42 // ## External Dependencies
45 // I disabled the following module (using a rather bad hack), because it only compiles if `docopt` is linked. However, before enabling it,
46 // you still have get the external library into the global namespace. This is done with `extern crate docopt;`, and that statement *has* to be
47 // in `main.rs`. So please go there, and enable this commented-out line. Then remove the attribute of the following module.
48 #[cfg(feature = "disabled")]
50 // 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.
52 use part12::{run, Options, OutputMode};
55 // The USAGE string documents how the program is to be called. It's written in a format that `docopt` can parse.
56 static USAGE: &'static str = "
57 Usage: rgrep [-c] [-s] <pattern> <file>...
60 -c, --count Count number of matching lines (rather than printing them).
61 -s, --sort Sort the lines before printing.
64 // This function extracts the rgrep options from the command-line arguments.
65 fn get_options() -> Options {
66 // 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/).
67 let args = Docopt::new(USAGE).and_then(|d| d.parse()).unwrap_or_else(|e| e.exit());
68 // Now we can get all the values out.
69 let count = args.get_bool("-c");
70 let sort = args.get_bool("-s");
71 let pattern = args.get_str("<pattern>");
72 let files = args.get_vec("<file>");
74 println!("Setting both '-c' and '-s' at the same time does not make any sense.");
78 // We need to make the strings owned to construct the `Options` instance.
80 files: files.iter().map(|file| file.to_string()).collect(),
81 pattern: pattern.to_string(),
82 output_mode: if count { OutputMode::Count } else if sort { OutputMode::SortAndPrint } else { OutputMode::Print },
86 // Finally, we can call the `run` function from the previous part on the options extracted using `get_options`. Edit `main.rs` to call this function.
87 // You can now use `cargo run -- <pattern> <files>` to call your program, and see the argument parser and the threads we wrote previously in action!
93 // **Exercise 13.3**: Wouldn't it be nice if rgrep supported regular expressions? There's already a crate that does all the parsing and matching on regular
94 // expression, it's called [regex](https://crates.io/crates/regex). Add this crate to the dependencies of your workspace, add an option ("-r") to switch
95 // the pattern to regular-expression mode, and change `filter_lines` to honor this option. The documentation of regex is available from its crates.io site.