From fa7b67e1c1db6e62b357c17f75b3981754b9bc33 Mon Sep 17 00:00:00 2001 From: chenxuuu Date: Tue, 11 Jan 2022 10:21:34 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=B8=8D=E7=9F=A5=E9=81=93=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=98=AF=E4=B8=8D=E6=98=AFmdbook=EF=BC=8C=E5=85=88?= =?UTF-8?q?=E6=8C=89mdbook=E7=9A=84=E6=A0=BC=E5=BC=8F=E6=8A=8A=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=94=B9=E7=9A=84=E7=AC=A6=E5=90=88=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- book/contents/basic/compound-type/struct.md | 42 ++++++++++++--------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/book/contents/basic/compound-type/struct.md b/book/contents/basic/compound-type/struct.md index d55d76b9..ef4915c5 100644 --- a/book/contents/basic/compound-type/struct.md +++ b/book/contents/basic/compound-type/struct.md @@ -110,24 +110,30 @@ fn build_user(email: String, username: String) -> User { > 值的注意的是:`username`所有权被转移给了`user2`,导致了`user1`无法再被使用,但是并不代表`user1`内部的其它字段不能被继续使用,例如: ```rust - let user1 = User { - email: String::from("someone@example.com"), - username: String::from("someusername123"), - active: true, - sign_in_count: 1, - }; - - 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); +# #[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 { + active: user1.active, + username: user1.username, + email: String::from("another@example.com"), + sign_in_count: user1.sign_in_count, +}; +println!("{}", user1.active); +// 下面这行会报错 +println!("{:?}", user1); +# } ``` ## 结构体的内存排列