Browse Source

struct

pull/2/head
jingxun 3 years ago
parent
commit
d25f10d6e9
  1. 36
      day04_1/src/main.rs

36
day04_1/src/main.rs

@ -0,0 +1,36 @@
// #[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect = (20, 30);
println!("The area of the rect is {}", area(rect));
let rect1 = Rectangle {
width: 14,
height: 37,
};
println!("The area of the rect is {}", area_struct(&rect1));
// dbg!(&rect1);
// println!("rect1 is {:#?}",rect1)
let rect2 = Rectangle{
width:23,
height:59
};
println!("The area of the rect is {}",rect2.area());
}
fn area(dims: (u32, u32)) -> u32 {
dims.0 * dims.1
}
fn area_struct(rect: &Rectangle) -> u32 {
rect.width * rect.height
}
Loading…
Cancel
Save