X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/bae9e47884fdc3fc1a81fb4844572a832fcfb2ce..562558d25054c5be82f11acad0fbe53699de5b1c:/workspace/src/part13.rs diff --git a/workspace/src/part13.rs b/workspace/src/part13.rs index 3ef7785..311eba5 100644 --- a/workspace/src/part13.rs +++ b/workspace/src/part13.rs @@ -1,8 +1,9 @@ // Rust-101, Part 13: Slices, Arrays, External Dependencies -// ================= +// ======================================================== // ## Slices + pub fn sort(data: &mut [T]) { if data.len() < 2 { return; } @@ -10,9 +11,11 @@ pub fn sort(data: &mut [T]) { // making sure that everything on the left is no larger than the pivot, and everything on the right is no smaller. let mut lpos = 1; let mut rpos = data.len(); - /* Invariant: pivot is data[0]; everything with index (0,lpos) is <= pivot; [rpos,len) is >= pivot; lpos < rpos */ + /* Invariant: pivot is data[0]; everything with index (0,lpos) is <= pivot; + [rpos,len) is >= pivot; lpos < rpos */ loop { - // **Exercise 13.1**: Complete this Quicksort loop. You can use `swap` on slices to swap two elements. + // **Exercise 13.1**: Complete this Quicksort loop. You can use `swap` on slices to swap two elements. Write a + // test function for `sort`. unimplemented!() } @@ -24,7 +27,7 @@ pub fn sort(data: &mut [T]) { unimplemented!() } -// **Exercise 13.2*: Since `String` implements `PartialEq`, you can now change the function `output_lines` in the previous part +// **Exercise 13.2**: Since `String` implements `PartialEq`, you can now change the function `output_lines` in the previous part // 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 // only, not by filename or line number! @@ -35,16 +38,16 @@ fn sort_nums(data: &mut Vec) { // ## Arrays fn sort_array() { - let mut data: [f64; 5] = [1.0, 3.4, 12.7, -9.12, 0.1]; - sort(&mut data); + let mut array_of_data: [f64; 5] = [1.0, 3.4, 12.7, -9.12, 0.1]; + sort(&mut array_of_data); } // ## External Dependencies // 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 following module. +// 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. @@ -52,7 +55,7 @@ pub mod rgrep { 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. + // 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] ... @@ -63,7 +66,7 @@ Options: // 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/). + // 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/). 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"); @@ -76,10 +79,17 @@ Options: } // We need to make the strings owned to construct the `Options` instance. + let mode = if count { + OutputMode::Count + } else if sort { + OutputMode::SortAndPrint + } else { + OutputMode::Print + }; Options { files: files.iter().map(|file| file.to_string()).collect(), pattern: pattern.to_string(), - output_mode: if count { OutputMode::Count } else if sort { OutputMode::SortAndPrint } else { OutputMode::Print }, + output_mode: mode, } } @@ -93,4 +103,5 @@ Options: // **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 // 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 // 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. +// (You won't be able to use the `regex!` macro if you are on the stable or beta channel of Rust. But it wouldn't help for our use-case anyway.)