workspaces and iterators

This commit is contained in:
gbrochar 2020-12-06 21:55:06 +01:00
parent 3c0bdf7b3c
commit 7ea306ec32
12 changed files with 169 additions and 43 deletions

9
iterators/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "iterators"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

51
iterators/src/main.rs Normal file
View File

@ -0,0 +1,51 @@
#[derive(PartialEq, Debug)]
struct Shoe {
size: u32,
style: String,
}
fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
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() {
}

View File

@ -9,12 +9,18 @@ pub struct Config {
}
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
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<Config, &'static str> {
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<dyn Error>> {
@ -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());
}
}

View File

@ -4,9 +4,7 @@ use std::process;
use minigrep::Config;
fn main() {
let args: Vec<String> = 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);
});

7
workspaces/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[workspace]
members = [
"adder",
"add-one",
"add-two",
]

View File

@ -0,0 +1,10 @@
[package]
name = "add-one"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.5.5"

View File

@ -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));
}
}

View File

@ -0,0 +1,9 @@
[package]
name = "add-two"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -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));
}
}

View File

@ -0,0 +1,12 @@
[package]
name = "adder"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
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"

View File

@ -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),
)
}

3
workspaces/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}