From fb38f07d8022386d1879ce6da858a67ae80ad665 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Sun, 21 Dec 2025 12:07:41 +0000 Subject: [PATCH] structs and methods --- methods/Cargo.toml | 6 ++ methods/src/main.rs | 37 ++++++++++++ picasso/Cargo.toml | 6 ++ picasso/src/main.rs | 132 ++++++++++++++++++++++++++++++++++++++++++ rectangle/Cargo.toml | 6 ++ rectangle/src/main.rs | 54 +++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 methods/Cargo.toml create mode 100644 methods/src/main.rs create mode 100644 picasso/Cargo.toml create mode 100644 picasso/src/main.rs create mode 100644 rectangle/Cargo.toml create mode 100644 rectangle/src/main.rs diff --git a/methods/Cargo.toml b/methods/Cargo.toml new file mode 100644 index 0000000..c4ffcf5 --- /dev/null +++ b/methods/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "methods" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/methods/src/main.rs b/methods/src/main.rs new file mode 100644 index 0000000..cfdbe52 --- /dev/null +++ b/methods/src/main.rs @@ -0,0 +1,37 @@ +struct Rectangle { + w: u32, + h: u32, +} + +impl Rectangle { + fn area(&self) -> u32 { + self.w * self.h + } + + fn can_hold(&self, vs: &Rectangle) -> bool { + self.w > vs.w && self.h > vs.h + } + + fn square(size: u32) -> Self { + Self { w: size, h: size } + } +} + +fn main() { + let rect1 = Rectangle { w: 30, h: 50 }; + let rect2 = Rectangle { w: 10, h: 40 }; + let rect3 = Rectangle { w: 60, h: 45 }; + + println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); + println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); + + println!("---\nMethod calls are syntactic sugar for functions:"); + println!( + "in other words, + rect1.area() which gives {} + is the same as + Rectangle::area(&rect1) which gives {}", + rect1.area(), + Rectangle::area(&rect1) + ); +} diff --git a/picasso/Cargo.toml b/picasso/Cargo.toml new file mode 100644 index 0000000..fa6f466 --- /dev/null +++ b/picasso/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "picasso" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/picasso/src/main.rs b/picasso/src/main.rs new file mode 100644 index 0000000..27ff7e7 --- /dev/null +++ b/picasso/src/main.rs @@ -0,0 +1,132 @@ +// picasso: take 3 rectangles from user +// if none of the 3 rectangles fit in one another +// user wins: picasso! +// else, inform user that they lost and how + +use std::io; + +#[derive(Debug)] +struct Rectangle { + w: i32, + h: i32, +} + +fn build() -> Rectangle { + let mut w = String::new(); + let mut h = String::new(); + + println!("\tplease enter width:"); + + io::stdin() + .read_line(&mut w) + .expect("failed to read user input"); + + let w: i32 = match w.trim().parse() { + Ok(num) => num, + Err(_) => todo!(), + }; + + println!("\tplease enter height:"); + + io::stdin() + .read_line(&mut h) + .expect("failed to read user input"); + + let h: i32 = match h.trim().parse() { + Ok(num) => num, + Err(_) => todo!(), + }; + + Rectangle { w, h } +} + +fn main() { + print_rules(); + + println!("\nenter details of rectangle 1:"); + let r1: Rectangle = build(); + + println!("\nenter details of rectangle 2:"); + let r2: Rectangle = build(); + + println!("\nenter details of rectangle 3:"); + let r3: Rectangle = build(); + + let result: [bool; 6] = compare(&r1, &r2, &r3); + + print_result(&result); +} +fn compare(r1: &Rectangle, r2: &Rectangle, r3: &Rectangle) -> [bool; 6] { + let mut result: [bool; 6] = [false; 6]; + + // r2 can fit in r1 + if r1.w > r2.w && r1.h > r2.h { + result[0] = true; + }; + // r1 can fit in r2 + if r1.w < r2.w && r1.h < r2.h { + result[1] = true; + }; + + // r3 in r2 + if r2.w > r3.w && r2.h > r3.h { + result[2] = true; + }; + // r2 can fit in r3 + if r2.w < r3.w && r2.h < r3.h { + result[3] = true; + }; + + // r3 in r1 + if r1.w > r3.w && r1.h > r3.h { + result[4] = true; + }; + //r1 can fit in r3 + if r1.w < r3.w && r1.h < r3.h { + result[5] = true; + }; + + result +} + +fn print_rules() { + println!("welcome to picasso!"); + println!("\nthe rules are simple:"); + println!("\n\tenter dimensions of 3 rectangles such that"); + println!("\n\tnone of the rectangles can fit in any other"); + println!("\n\tif any of the rectangles fit in any other one, you lose!"); + println!("\nReady?"); +} + +fn print_result(result: &[bool; 6]) { + if *result == [false; 6] { + println!("\n\t-----picasso!-----"); + return; + } + + if result[0] == true { + println!("...r2 can fit in r1"); + }; + if result[1] == true { + println!("...r1 can fit in r2"); + }; + if result[2] == true { + println!("...r3 can fit in r2"); + }; + if result[3] == true { + println!("...r2 can fit in r3"); + }; + if result[4] == true { + println!("...r3 can fit in r1"); + }; + if result[5] == true { + println!("...r1 can fit in r3"); + }; +} + +// i see a lot of improvements +// first, we can use a struct to store the results +// iterate over the struct, if anything is true then print the struct name +// make the build function more idiomatic (chapter 13 has a nice build function we can borrow from) +// add tests for all 6 cases +// show the user a simulation of the rectangles! diff --git a/rectangle/Cargo.toml b/rectangle/Cargo.toml new file mode 100644 index 0000000..7693d91 --- /dev/null +++ b/rectangle/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rectangle" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rectangle/src/main.rs b/rectangle/src/main.rs new file mode 100644 index 0000000..fd6c07e --- /dev/null +++ b/rectangle/src/main.rs @@ -0,0 +1,54 @@ +// Building up to structs +// +// fn main() { +// let width1 = 30; +// let height1 = 50; +// +// println!( +// "The area of the rectangle is {} square pixels", +// area(width1, height1) +// ); +// } +// +// fn area(w: u32, h: u32) -> u32 { +// w * h +// } + +// fn main() { +// let rect1 = (30, 50); +// +// println!("The area of the rectangle is {} square pixels", area(rect1)) +// } +// +// fn area(dims: (u32, u32)) -> u32 { +// dims.0 * dims.1 +// } + +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn area(rec: &Rectangle) -> u32 { + rec.width * rec.height +} + +fn main() { + let scale = 3; + let my_rec = Rectangle { + width: dbg!(30 * scale), + height: 40, + }; + + dbg!(&my_rec); + + // println!( + // "The area of the rectangle is {} square pixels", + // area(&my_rec) + // ); + // + // println!("rectangle is {my_rec:?}"); + // println!("--------"); + // println!("rectangle is {my_rec:#?}"); +}