structs and methods

This commit is contained in:
2025-12-21 12:07:41 +00:00
parent 04c6e7e66a
commit fb38f07d80
6 changed files with 241 additions and 0 deletions

6
rectangle/Cargo.toml Normal file
View File

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

54
rectangle/src/main.rs Normal file
View 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:#?}");
}