From 7ea306ec32beb7d5a5e688dbef455220c32e666c Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 6 Dec 2020 21:55:06 +0100 Subject: [PATCH] workspaces and iterators --- iterators/Cargo.toml | 9 ++++++ iterators/src/main.rs | 51 +++++++++++++++++++++++++++++ minigrep/src/lib.rs | 60 ++++++++++++----------------------- minigrep/src/main.rs | 4 +-- workspaces/Cargo.toml | 7 ++++ workspaces/add-one/Cargo.toml | 10 ++++++ workspaces/add-one/src/lib.rs | 13 ++++++++ workspaces/add-two/Cargo.toml | 9 ++++++ workspaces/add-two/src/lib.rs | 13 ++++++++ workspaces/adder/Cargo.toml | 12 +++++++ workspaces/adder/src/main.rs | 21 ++++++++++++ workspaces/src/main.rs | 3 ++ 12 files changed, 169 insertions(+), 43 deletions(-) create mode 100644 iterators/Cargo.toml create mode 100644 iterators/src/main.rs create mode 100644 workspaces/Cargo.toml create mode 100644 workspaces/add-one/Cargo.toml create mode 100644 workspaces/add-one/src/lib.rs create mode 100644 workspaces/add-two/Cargo.toml create mode 100644 workspaces/add-two/src/lib.rs create mode 100644 workspaces/adder/Cargo.toml create mode 100644 workspaces/adder/src/main.rs create mode 100644 workspaces/src/main.rs diff --git a/iterators/Cargo.toml b/iterators/Cargo.toml new file mode 100644 index 0000000..8a72f5c --- /dev/null +++ b/iterators/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "iterators" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/iterators/src/main.rs b/iterators/src/main.rs new file mode 100644 index 0000000..3b7a07f --- /dev/null +++ b/iterators/src/main.rs @@ -0,0 +1,51 @@ +#[derive(PartialEq, Debug)] +struct Shoe { + size: u32, + style: String, +} + +fn shoes_in_my_size(shoes: Vec, shoe_size: u32) -> Vec { + shoes.into_iter().filter(|s| s.size == shoe_size).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn filters_by_size() { + let shoes = vec![ + Shoe { + size: 10, + style: String::from("sneaker"), + }, + Shoe { + size: 13, + style: String::from("sandal"), + }, + Shoe { + size: 10, + style: String::from("boot"), + }, + ]; + + let in_my_size = shoes_in_my_size(shoes, 10); + + assert_eq!( + in_my_size, + vec![ + Shoe { + size: 10, + style: String::from("sneaker") + }, + Shoe { + size: 10, + style: String::from("boot") + }, + ] + ); + } +} + +fn main() { +} \ No newline at end of file diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs index 176a67d..e6ccf76 100644 --- a/minigrep/src/lib.rs +++ b/minigrep/src/lib.rs @@ -9,12 +9,18 @@ pub struct Config { } impl Config { - pub fn new(args: &[String]) -> Result { - if args.len() < 3 { - return Err("not enough arguments"); - } - let query = args[1].clone(); - let filename = args[2].clone(); + pub fn new(mut args: env::Args) -> Result { + args.next(); + + let query = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a query string"), + }; + + let filename = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a file name"), + }; let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); @@ -23,28 +29,19 @@ impl Config { } fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { - let mut results = Vec::new(); - - for line in contents.lines() { - if line.contains(query) { - results.push(line); - } - } - - results + contents + .lines() + .filter(|line| line.contains(query)) + .collect() } fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { - let mut results = Vec::new(); let query = query.to_lowercase(); - for line in contents.lines() { - if line.to_lowercase().contains(&query) { - results.push(line); - } - } - - results + contents + .lines() + .filter(|line| line.to_lowercase().contains(&query)) + .collect() } pub fn run(config: Config) -> Result<(), Box> { @@ -111,21 +108,4 @@ Trust me."; case_sensitive: true, }).is_err()); } - - #[test] - fn config_new_valid() { - assert!(Config::new( - &vec![ - String::from("binaryname"), - String::from("query"), - String::from("filename")]).is_ok()); - } - - #[test] - fn config_new_invalid() { - assert!(Config::new( - &vec![ - String::from("only two"), - String::from("arguments")]).is_err()); - } } \ No newline at end of file diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs index d52c4f1..61c3f02 100644 --- a/minigrep/src/main.rs +++ b/minigrep/src/main.rs @@ -4,9 +4,7 @@ use std::process; use minigrep::Config; fn main() { - let args: Vec = env::args().collect(); - - let config = Config::new(&args).unwrap_or_else(|err| { + let config = Config::new(env::args()).unwrap_or_else(|err| { eprintln!("Problem parsing arguments: {}", err); process::exit(1); }); diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml new file mode 100644 index 0000000..375a237 --- /dev/null +++ b/workspaces/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] + +members = [ + "adder", + "add-one", + "add-two", +] diff --git a/workspaces/add-one/Cargo.toml b/workspaces/add-one/Cargo.toml new file mode 100644 index 0000000..1eaa90a --- /dev/null +++ b/workspaces/add-one/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "add-one" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.5.5" \ No newline at end of file diff --git a/workspaces/add-one/src/lib.rs b/workspaces/add-one/src/lib.rs new file mode 100644 index 0000000..87c7a34 --- /dev/null +++ b/workspaces/add-one/src/lib.rs @@ -0,0 +1,13 @@ +pub fn add_one(n: i32) -> i32 { + n + 1 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + assert_eq!(3, add_one(2)); + } +} \ No newline at end of file diff --git a/workspaces/add-two/Cargo.toml b/workspaces/add-two/Cargo.toml new file mode 100644 index 0000000..04f5677 --- /dev/null +++ b/workspaces/add-two/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "add-two" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/workspaces/add-two/src/lib.rs b/workspaces/add-two/src/lib.rs new file mode 100644 index 0000000..9fc89df --- /dev/null +++ b/workspaces/add-two/src/lib.rs @@ -0,0 +1,13 @@ +pub fn add_two(n: i32) -> i32 { + n + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + assert_eq!(4, add_two(2)); + } +} \ No newline at end of file diff --git a/workspaces/adder/Cargo.toml b/workspaces/adder/Cargo.toml new file mode 100644 index 0000000..37826a3 --- /dev/null +++ b/workspaces/adder/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "adder" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +add-one = { path = "../add-one" } +add-two = { path = "../add-two" } +rand = "0.5.5" \ No newline at end of file diff --git a/workspaces/adder/src/main.rs b/workspaces/adder/src/main.rs new file mode 100644 index 0000000..d1c85ec --- /dev/null +++ b/workspaces/adder/src/main.rs @@ -0,0 +1,21 @@ +use rand::Rng; +use add_one; +use add_two; + +fn main() { + let num = 10; + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num), + ); + println!( + "Hello, world! {} plus two is {}!", + num, + add_two::add_two(num), + ); + println!( + "And here is a random number between 1 and 100: {}", + rand::thread_rng().gen_range(1, 101), + ) +} \ No newline at end of file diff --git a/workspaces/src/main.rs b/workspaces/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/workspaces/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}