Skip to content

Ch05-01 - Misleading Text regarding borrowed Struct props #2978

Closed
rust-lang/rust
#104171
@dvreed77

Description

@dvreed77

URL to the section(s) of the book with this problem:

Interact: Move”][move]<!-- ignore --> section. In this example, we can no

Description of the problem:

The following text isn't accurate

In this example, we can no longer use user1 after creating user2 because the String in the username field of user1 was moved into user2

You can still use all of user1, except the username property. For example this code works:

struct User {
    active: bool,
    username: String,
    email: String,
    sign_in_count: u64,
}

fn main() {
    let user1 = User {
        email: String::from("[email protected]"),
        username: String::from("someusername123"),
        active: true,
        sign_in_count: 1,
    };

    let user2 = User {
        email: String::from("[email protected]"),
        ..user1
    };

    println!("{}", user1.email);
    println!("{}", user2.email);
}

Suggested fix:

In this example, we can no longer use the username property of user1 after creating user2 because the String in the username field of user1 was moved into user2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions