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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/*
// Regular struct
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
// Tuple Struct
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
// Unit-Like Structs
struct AlwaysEqual;
fn main() {
let user1 = build_user(String::from("root"), String::from("root@example.com"));
println!("{}", user1.username);
let user2 = User {
email: String::from("another@example.com"),
..user1
};
println!("{}", user2.username);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
println!("{} {}", black.0, origin.1);
let subject = AlwaysEqual;
}
fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
*/
/*
fn main() {
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
}
*/
/*
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
}
*/
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale),
height: 50,
};
println!("rect1 is {rect1:?}");
println!("rect1 is {rect1:#?}");
dbg!(&rect1);
println!(
"The area of the rectangle is {} square pixels.",
area(&rect1)
);
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}
|