You've already forked book-exercises
Compare commits
3 Commits
c258b53e52
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d40af5a56 | |||
| c1b1d8e615 | |||
| fd51666ea8 |
6
closures/Cargo.toml
Normal file
6
closures/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "closures"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
63
closures/src/main.rs
Normal file
63
closures/src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
// giveaway tshirts to users
|
||||||
|
// if someone has a favorite colour they get that
|
||||||
|
// else they get whatever colour we currently have the most of
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
|
enum ShirtColor {
|
||||||
|
Red,
|
||||||
|
Blue,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Inventory {
|
||||||
|
shirts: Vec<ShirtColor>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[derive(Debug)]
|
||||||
|
// struct User {
|
||||||
|
// shirt_preference: Option<ShirtColor>,
|
||||||
|
// }
|
||||||
|
|
||||||
|
impl Inventory {
|
||||||
|
fn giveaway(&self, user_pref: Option<ShirtColor>) -> ShirtColor {
|
||||||
|
user_pref.unwrap_or_else(|| self.current_max())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_max(&self) -> ShirtColor {
|
||||||
|
let mut red = 0;
|
||||||
|
let mut blue = 0;
|
||||||
|
for color in &self.shirts {
|
||||||
|
match color {
|
||||||
|
ShirtColor::Red => red += 1,
|
||||||
|
ShirtColor::Blue => blue += 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if red > blue {
|
||||||
|
return ShirtColor::Red;
|
||||||
|
} else {
|
||||||
|
return ShirtColor::Blue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let current_inventory = Inventory {
|
||||||
|
shirts: vec![
|
||||||
|
ShirtColor::Red,
|
||||||
|
ShirtColor::Red,
|
||||||
|
ShirtColor::Blue,
|
||||||
|
ShirtColor::Blue,
|
||||||
|
ShirtColor::Blue,
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("DEBUG: inventory: {:?}", current_inventory);
|
||||||
|
|
||||||
|
let user_red = Some(ShirtColor::Red);
|
||||||
|
let giveaway1 = current_inventory.giveaway(user_red);
|
||||||
|
println!("DEBUG: giveaway {:?} to {:?}", giveaway1, user_red);
|
||||||
|
|
||||||
|
let user_none = None;
|
||||||
|
let giveaway2 = current_inventory.giveaway(user_none);
|
||||||
|
println!("DEBUG: giveaway {:?} to {:?}", giveaway2, user_none);
|
||||||
|
}
|
||||||
6
minigrep/Cargo.toml
Normal file
6
minigrep/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "minigrep"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
10
minigrep/poems.txt
Normal file
10
minigrep/poems.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
I'm nobody! Who are you?
|
||||||
|
Are you nobody, too?
|
||||||
|
Then there's a pair of us - don't tell!
|
||||||
|
They'd banish us, you know.
|
||||||
|
|
||||||
|
How dreary to be somebody!
|
||||||
|
How public, like a frog
|
||||||
|
To tell your name the livelong day
|
||||||
|
To an admiring bog!
|
||||||
|
|
||||||
102
minigrep/src/lib.rs
Normal file
102
minigrep/src/lib.rs
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
|
let contents = fs::read_to_string(config.file_path)?;
|
||||||
|
|
||||||
|
let mut results;
|
||||||
|
let casing = (config.ignore_case_arg, config.ignore_case_env);
|
||||||
|
|
||||||
|
// only perform case sensitive search if both
|
||||||
|
// ignore_case_env and ignore_case_arg are false
|
||||||
|
match casing {
|
||||||
|
(false, false) => results = search(&config.query, &contents),
|
||||||
|
(_, _) => results = search_case_insensitive(&config.query, &contents),
|
||||||
|
}
|
||||||
|
|
||||||
|
for line in results {
|
||||||
|
println!("{line}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
pub query: String,
|
||||||
|
pub file_path: String,
|
||||||
|
pub ignore_case_arg: bool,
|
||||||
|
pub ignore_case_env: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn build(args: &[String]) -> Result<Config, &'static str> {
|
||||||
|
if args.len() < 4 {
|
||||||
|
return Err("not enough arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
let query = args[1].clone();
|
||||||
|
let file_path = args[2].clone();
|
||||||
|
let ignore_case_arg = !args[3].clone().is_empty();
|
||||||
|
let ignore_case_env = env::var("IGNORE_CASE").is_ok();
|
||||||
|
|
||||||
|
Ok(Config {
|
||||||
|
query,
|
||||||
|
file_path,
|
||||||
|
ignore_case_arg,
|
||||||
|
ignore_case_env,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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
|
||||||
|
}
|
||||||
|
pub 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
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_sensitive() {
|
||||||
|
let query = "duct";
|
||||||
|
let contents = "Rust:
|
||||||
|
safe, fast, productive.
|
||||||
|
Pick three.
|
||||||
|
Duct tape.";
|
||||||
|
|
||||||
|
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_insensitive() {
|
||||||
|
let query = "rUsT";
|
||||||
|
let contents = "Rust:
|
||||||
|
safe, fast, productive.
|
||||||
|
Pick three.
|
||||||
|
Trust me.";
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec!["Rust:", "Trust me."],
|
||||||
|
search_case_insensitive(query, contents)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
minigrep/src/main.rs
Normal file
34
minigrep/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use minigrep::Config;
|
||||||
|
use std::env;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
let config = Config::build(&args).unwrap_or_else(|error| {
|
||||||
|
println!("Problem parsing arguments: {error}");
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Err(e) = minigrep::run(config) {
|
||||||
|
println!("Application error: {e}");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of main
|
||||||
|
// another way to handle the Config building
|
||||||
|
// let config = Config::build(&args);
|
||||||
|
// match config {
|
||||||
|
// Err(error) => {
|
||||||
|
// println!("error: {error}");
|
||||||
|
// }
|
||||||
|
// Ok(config) => {
|
||||||
|
// println!(
|
||||||
|
// "searching for \"{}\" in file \"{}\"",
|
||||||
|
// config.query, config.file_path,
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// println!("with text:\n{contents}");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user