-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_structs.rs
48 lines (37 loc) · 1.01 KB
/
example_structs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// fn main() {
// println!("Hello, world!");
// let width1 = 30;
// let height1 = 50;
// println!(
// "The area of the rectangle is {} square pixels.",
// area(width1, height1)
// );
// }
// fn area(width: u32, height: u32) -> u32 {
// width * height
// }
////////////////////////////////////////////////////////////////////
// refactoring with tuples
// fn main() {
// let rect1 = (30,50);
// println!("The area of the rectangle is {} square pixels.",area(rect1));
// }
// fn area(dimensions: (u32, u32)) -> u32 {
// dimensions.0 * dimensions.1
// }
///////////////////////////////////////////////////////////////////////
//Refactoring with Structs: Adding More Meaning
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!("The area of the rectangle is {} square pixels.",area(&rect1));
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}