-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
134 lines (117 loc) · 3.44 KB
/
main.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[derive(Debug)]
struct User {
active: bool,
username: String,
email: String,
signin_count: u64,
}
fn main() {
let user1 = User {
active: true,
email: String::from("[email protected]"),
username: String::from("someone"),
signin_count: 1,
};
println!("User1: {:?}", user1);
println!("mutable struct");
mut_struct();
println!("new user");
let u = new_user();
println!("new user {:?}", u);
println!("new user 1 - struct init short hand");
let u = new_user_1(String::from("someone"), String::from("[email protected]"));
println!("new user {:?}", u);
println!("struct_update_manual");
struct_update_1();
println!("struct_update_syntax");
struct_update_sytax();
println!("tuple structs");
tuple_structs();
println!("unit struct");
unit_struct();
}
// Individual fields cannot be made mutable. The
// entire struct needs to be
fn mut_struct() {
let mut user1 = User {
active: true,
email: String::from("[email protected]"),
username: String::from("someone"),
signin_count: 1,
};
user1.signin_count = 2;
println!("User1: {:?}", user1);
}
// new user with implicit return
fn new_user() -> User {
User {
active: false,
email: String::from("[email protected]"),
username: String::from("anonymous"),
signin_count: 0,
}
}
// new user wtih username and email arguments
// (same name as struct members) using init short hand.
fn new_user_1(username: String, email: String) -> User {
User {
username,
email,
active: false,
signin_count: 0,
}
}
// update/create struct using other struct instance
// manual method
fn struct_update_1() {
let u1 = User {
username: String::from("someone"),
email: String::from("[email protected]"),
active: false,
signin_count: 0,
};
let u1a = User {
username: String::clone(&u1.username),
email: String::clone(&u1.email),
active: true,
signin_count: 1,
};
println!("User1 : {:?}", u1);
println!("User1a: {:?}", u1a);
}
// struct update is like assignment (=), which means the values
// are moved. Used in cases where u1 is no longer useful. The values
// for `username` and `email` are moved. The other two are scalar types
// which implement the copy trait and hence stays on the stack.
fn struct_update_sytax() {
let u1 = User {
username: String::from("someone"),
email: String::from("[email protected]"),
active: false,
signin_count: 0,
};
let u2 = User {
active: true,
signin_count: 1,
..u1 // value moved
};
println!("User2: {:?}", u2);
}
// structs are similar to tuples with names associated to fields
// tuple structs are similar to tuples - the difference being
// each tuple struct type is different type and identified by a name.
fn tuple_structs() {
struct Point(i32, i32, i32);
struct Color(i32, i32, i32);
let zero = Point(0, 0, 0);
let black = Color(0, 0, 0);
println!("point: x, y, z => {}, {}, {}", zero.0, zero.1, zero.2);
println!("black: r, g, b => {}, {}, {}", black.0, black.1, black.2);
}
// unit like struct
// just like a tuple unit. Unit like struct does not have fields.
fn unit_struct() {
struct AlwaysEqual;
let _result = AlwaysEqual;
// this can be used to implement traits with no values.
}