You've already forked book-exercises
structs and methods
This commit is contained in:
6
methods/Cargo.toml
Normal file
6
methods/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "methods"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
37
methods/src/main.rs
Normal file
37
methods/src/main.rs
Normal file
@@ -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)
|
||||
);
|
||||
}
|
||||
6
picasso/Cargo.toml
Normal file
6
picasso/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "picasso"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
132
picasso/src/main.rs
Normal file
132
picasso/src/main.rs
Normal file
@@ -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!
|
||||
6
rectangle/Cargo.toml
Normal file
6
rectangle/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rectangle"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
54
rectangle/src/main.rs
Normal file
54
rectangle/src/main.rs
Normal file
@@ -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:#?}");
|
||||
}
|
||||
Reference in New Issue
Block a user