Compare commits

...

6 Commits

Author SHA1 Message Date
6d40af5a56 learnings from seeing listing 13-1
- i was trying to do too much!
- the book keeps it to two colours
- it does not update inventory after each giveaway
- it does not implement a `User` struct
- "Simplicity is key." - DJ Khaled
2026-01-24 07:08:06 +00:00
c1b1d8e615 closures
- try to implement a quick and dirty solution for listing 13-1 myself
2026-01-24 06:43:09 +00:00
fd51666ea8 minigrep 2026-01-23 08:53:52 +00:00
c258b53e52 generics and lifetimes 2026-01-22 01:54:12 +00:00
b8c7b5e68b finish testing
- convert project to a library by moving program logic into lib.rs
- create ./tests to store integration tests
- create ./common to store code we want shared across integration tests
2026-01-19 08:01:27 +00:00
b287268919 testing
- create regular functions with various return types
- create test functions with various return types
- experiment with `cargo test`, `#[should_panic]`, `#[ignore]` and test modules
2026-01-19 07:06:25 +00:00
18 changed files with 611 additions and 0 deletions

6
closures/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "closures"
version = "0.1.0"
edition = "2024"
[dependencies]

63
closures/src/main.rs Normal file
View 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);
}

View File

@@ -0,0 +1,6 @@
[package]
name = "generics"
version = "0.1.0"
edition = "2024"
[dependencies]

View 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
}

View File

@@ -0,0 +1,6 @@
[package]
name = "media-aggregator"
version = "0.1.0"
edition = "2024"
[dependencies]

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

View 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
View File

@@ -0,0 +1,6 @@
[package]
name = "lifetimes"
version = "0.1.0"
edition = "2024"
[dependencies]

26
lifetimes/src/main.rs Normal file
View 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
View File

@@ -0,0 +1,6 @@
[package]
name = "minigrep"
version = "0.1.0"
edition = "2024"
[dependencies]

10
minigrep/poems.txt Normal file
View 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
View 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
View 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}");
// }
// }

6
testing/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "testing"
version = "0.1.0"
edition = "2024"
[dependencies]

175
testing/src/lib.rs Normal file
View File

@@ -0,0 +1,175 @@
pub fn print_operations(a: &i32, b: &i32) {
let sum = ops::add(&a, &b);
let difference = ops::sub(&a, &b);
let product = ops::mul(&a, &b);
let quotient = ops::div(&a, &b);
println!(
"
a is {a} and b is {b} and
sum = {sum}
difference = {difference}
product = {product}
quotient = {quotient}
"
);
}
pub mod ops {
pub fn add(a: &i32, b: &i32) -> i32 {
*a + *b
}
pub fn sub(a: &i32, b: &i32) -> i32 {
*a - *b
}
pub fn mul(a: &i32, b: &i32) -> i32 {
*a * *b
}
pub fn div(a: &i32, b: &i32) -> i32 {
*a / *b
}
}
pub mod users {
pub fn admin_checker(user: &str) -> bool {
if *user == "admin".to_string() {
true
} else {
panic!("bruh moment");
}
}
pub fn john_checker(user: &str) -> Result<bool, String> {
if *user == "john".to_string() {
Ok(true)
} else {
Err("panicking because you're not john!".to_string())
}
}
}
#[cfg(test)]
mod panic_tests {
use crate::users::{admin_checker, john_checker};
#[test]
#[should_panic(expected = "bruh")]
fn is_admin() {
let user = "notadmin";
admin_checker(user);
}
#[test]
fn is_john() -> Result<(), String> {
let user = "john";
let result = john_checker(user);
match result {
std::result::Result::Ok(true) => Ok(()),
std::result::Result::Ok(false) => {
Err("is_john test failed because it's someone else!".to_string())
}
Err(_) => Err("is_john test failed because it's someone else!".to_string()),
}
}
}
#[cfg(test)]
mod tests_returning_results {
use crate::ops::*;
#[test]
fn test_add() -> Result<(), String> {
let a = 12;
let b = 14;
let result = add(&a, &b);
if result == 26 {
Ok(())
} else {
Err("12 + 14 should be 26 but it's not".to_string())
}
}
#[test]
fn test_sub() -> Result<(), String> {
let a = 12;
let b = 14;
let result = sub(&a, &b);
if result == -2 {
Ok(())
} else {
Err("error calculating difference".to_string())
}
}
#[test]
#[ignore]
fn test_mul() -> Result<(), String> {
let a = 14;
let b = 12;
let result = mul(&a, &b);
if result == 168 {
Ok(())
} else {
Err("error calculating product".to_string())
}
}
#[test]
#[ignore]
fn test_div() -> Result<(), String> {
let a = 14;
let b = 12;
let result = div(&a, &b);
if result == 1 {
Ok(())
} else {
Err("error calculating quotient".to_string())
}
}
}
#[cfg(test)]
mod tests_returning_bool {
use crate::ops::*;
#[test]
fn test_add() {
let a = 12;
let b = 14;
let result = add(&a, &b);
assert_eq!(result, a + b, "12 + 14 should be 26 but it's not");
}
#[test]
fn test_sub() {
let a = 12;
let b = 14;
let result = sub(&a, &b);
assert_eq!(result, a - b);
}
#[test]
#[ignore]
fn test_mul() {
let a = 12;
let b = 14;
let result = mul(&a, &b);
assert_eq!(result, a * b);
}
#[test]
#[ignore]
fn test_div() {
let a = 12;
let b = 14;
let result = div(&a, &b);
assert_eq!(result, a / b);
}
}

19
testing/src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
use testing::print_operations;
use testing::users::*;
fn main() {
let a = 14;
let b = 12;
let user = "admin";
let john = "john";
print_operations(&a, &b);
if admin_checker(user) {
println!("you have the relevant permissions");
};
if john_checker(john).unwrap() {
println!("hi john!");
};
}

View File

@@ -0,0 +1,3 @@
pub fn give_me_5() -> usize {
5
}

View File

@@ -0,0 +1,24 @@
use testing::ops::*;
mod common;
#[test]
fn subtract_and_divide() {
let a = 20;
let b = 2;
let c = 3;
let difference = sub(&a, &b);
let quotient = div(&difference, &c);
assert_eq!(quotient, 6);
}
#[test]
fn add_and_multiply() {
let a = 10;
let b = common::give_me_5() as i32;
let c = 3;
let sum = add(&a, &b);
let product = mul(&sum, &c);
assert_eq!(product, 45);
}