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
|
// 结构体定义
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
// 结构体实例化
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
// 初始化结构体的简洁写法
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
// 更新创建,创建 user2 其余值来自 user1
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
// 元素访问
let x=user1.email;
|