|
|
|
@ -110,24 +110,30 @@ fn build_user(email: String, username: String) -> User {
|
|
|
|
|
> 值的注意的是:`username`所有权被转移给了`user2`,导致了`user1`无法再被使用,但是并不代表`user1`内部的其它字段不能被继续使用,例如:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let user1 = User {
|
|
|
|
|
# #[derive(Debug)]
|
|
|
|
|
# struct User {
|
|
|
|
|
# active: bool,
|
|
|
|
|
# username: String,
|
|
|
|
|
# email: String,
|
|
|
|
|
# sign_in_count: u64,
|
|
|
|
|
# }
|
|
|
|
|
# fn main() {
|
|
|
|
|
let user1 = User {
|
|
|
|
|
email: String::from("someone@example.com"),
|
|
|
|
|
username: String::from("someusername123"),
|
|
|
|
|
active: true,
|
|
|
|
|
sign_in_count: 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let user2 = User {
|
|
|
|
|
};
|
|
|
|
|
let user2 = User {
|
|
|
|
|
active: user1.active,
|
|
|
|
|
username: user1.username,
|
|
|
|
|
email: String::from("another@example.com"),
|
|
|
|
|
sign_in_count: user1.sign_in_count,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
println!("{}", user1.active);
|
|
|
|
|
|
|
|
|
|
// 下面这行会报错
|
|
|
|
|
//println!("{}", user1.username);
|
|
|
|
|
};
|
|
|
|
|
println!("{}", user1.active);
|
|
|
|
|
// 下面这行会报错
|
|
|
|
|
println!("{:?}", user1);
|
|
|
|
|
# }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 结构体的内存排列
|
|
|
|
|