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
|
let mut user1 = User {
active: true,
username: String::from("someusername123"),
email: String::from("someone@example.com"),
sign_in_count: 1,
};
// 字段初始化简写
fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
// user2 中的其他字段从user1中获取
// String 类型的数据被移动,user1 中不能再使用
let user2 = User {
email: String::from("another@example.com"),
..user1 // 必须放最后
};
// 元组结构体
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
// 类单元结构体,实现 trait
struct AlwaysEqual;
|