You've already forked book-exercises
structs and methods
This commit is contained in:
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