You've already forked book-exercises
Compare commits
4 Commits
b8c7b5e68b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d40af5a56 | |||
| c1b1d8e615 | |||
| fd51666ea8 | |||
| c258b53e52 |
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
generics/largest_number/Cargo.toml
Normal file
6
generics/largest_number/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "generics"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
31
generics/largest_number/src/main.rs
Normal file
31
generics/largest_number/src/main.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
fn main() {
|
||||
let number_list = vec![34, 50, 25, 100, 65];
|
||||
let largest = largest_number(&number_list);
|
||||
println!("The largest number is {largest}");
|
||||
// for number in &number_list {
|
||||
// if number > largest {
|
||||
// largest = number;
|
||||
// }
|
||||
// }
|
||||
|
||||
let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];
|
||||
let largest = largest_number(&number_list);
|
||||
println!("The largest number is {largest}");
|
||||
// let mut largest = &number_list[0];
|
||||
// for number in &number_list {
|
||||
// if number > largest {
|
||||
// largest = number;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
fn largest_number(list: &[i32]) -> &i32 {
|
||||
let mut largest = &list[0];
|
||||
|
||||
for number in list {
|
||||
if number > largest {
|
||||
largest = number;
|
||||
}
|
||||
}
|
||||
largest
|
||||
}
|
||||
6
generics/media-aggregator/Cargo.toml
Normal file
6
generics/media-aggregator/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "media-aggregator"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
60
generics/media-aggregator/src/lib.rs
Normal file
60
generics/media-aggregator/src/lib.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use ::std::fmt::Display;
|
||||
use std::fmt::Debug;
|
||||
pub trait Summary {
|
||||
fn summarize_author(&self) -> String;
|
||||
|
||||
fn summarize(&self) -> String {
|
||||
// "(Read more...)".to_string()
|
||||
let x = "a".to_string();
|
||||
x
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NewsArticle {
|
||||
pub headline: String,
|
||||
pub location: String,
|
||||
pub author: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SocialPost {
|
||||
pub username: String,
|
||||
pub content: String,
|
||||
pub reply: bool,
|
||||
pub repost: bool,
|
||||
}
|
||||
|
||||
impl Summary for NewsArticle {
|
||||
// fn summarize(&self) -> String {
|
||||
// format!("{}, by {} ({})", self.headline, self.author, self.location)
|
||||
// }
|
||||
fn summarize_author(&self) -> String {
|
||||
"trad media".to_string() // format!("@{}", self.author)
|
||||
}
|
||||
}
|
||||
|
||||
// impl Summary for SocialPost {
|
||||
// fn summarize(&self) -> String {
|
||||
// format!("{} by {}", self.content, self.summarize_author())
|
||||
// }
|
||||
//
|
||||
// fn summarize_author(&self) -> String {
|
||||
// format!("@{}", self.username)
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub fn notify(item: &impl Summary) {
|
||||
// println!("{}", item.summarize());
|
||||
// }
|
||||
|
||||
pub fn notify<T, U>(item: &T, x: U)
|
||||
where
|
||||
T: Summary,
|
||||
U: Copy + Debug + Display + Clone,
|
||||
{
|
||||
println!("{}", item.summarize());
|
||||
let y = x;
|
||||
println!("doubled: {:?}", y);
|
||||
}
|
||||
28
generics/media-aggregator/src/main.rs
Normal file
28
generics/media-aggregator/src/main.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use media_aggregator::{NewsArticle, SocialPost};
|
||||
use media_aggregator::{Summary, notify};
|
||||
|
||||
fn main() {
|
||||
let article = NewsArticle {
|
||||
headline: "Chancellor on brink of second bailout for banks".to_string(),
|
||||
location: "London".to_string(),
|
||||
author: "The Times".to_string(),
|
||||
content: "Please subscribe to Premium to read the whole story.".to_string(),
|
||||
};
|
||||
|
||||
let tweet = SocialPost {
|
||||
content: "running bitcoin".to_string(),
|
||||
username: "halfin".to_string(),
|
||||
reply: false,
|
||||
repost: false,
|
||||
};
|
||||
|
||||
println!("\n --- DEBUG: values ---");
|
||||
println!("{:?}", article);
|
||||
// println!("{:?}", tweet);
|
||||
|
||||
println!("\n --- DEBUG: article summary ---");
|
||||
println!("{:?}", article.summarize());
|
||||
|
||||
println!("\n --- DEBUG: article notify---");
|
||||
notify(&article, 5);
|
||||
}
|
||||
6
lifetimes/Cargo.toml
Normal file
6
lifetimes/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "lifetimes"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
26
lifetimes/src/main.rs
Normal file
26
lifetimes/src/main.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
// fn main() {
|
||||
// let string1 = String::from("abcd");
|
||||
// let string2 = String::from("xyz");
|
||||
// let result = longest(string1.as_str(), string2.as_str());
|
||||
// println!("The longer string is: {result}");
|
||||
// }
|
||||
//
|
||||
// fn longest<'somelifetime>(a: &'somelifetime str, b: &'somelifetime str) -> &'somelifetime str {
|
||||
// if a.len() > b.len() { a } else { b }
|
||||
// }
|
||||
//
|
||||
struct ImportantExcerpt<'a> {
|
||||
part: &'a str,
|
||||
}
|
||||
//
|
||||
// fn return_excerpt(story: ImportantExcerpt) -> &'a str {
|
||||
// let first_sentence = novel.split('.')
|
||||
// }
|
||||
|
||||
fn main() {
|
||||
let novel = String::from("Call me Ishmael. Some years ago...");
|
||||
let first_sentence = novel.split('.').next().unwrap();
|
||||
let i = ImportantExcerpt {
|
||||
part: first_sentence,
|
||||
};
|
||||
}
|
||||
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