Skip to content

[Moving Values] Accessing fields of a borrowed struct instance #4402

Open
@paschaldev

Description

@paschaldev

In Chapter 5 (An Example program using stucts), the example uses a scalar type u32 for the Rectangle struct with this note underneath

... note that accessing fields of a borrowed struct instance does not move the field values, which is why you often see borrows of structs

https://doc.rust-lang.org/book/ch05-02-example-structs.html

I tried to write the Struct and referenced a string in the update_user function as seen in the image below. The compiler raised an error

   |   ^^^^^^^^^ move occurs because `user.name` has type `String`, which does not implement the `Copy` trait

Is that statement correct or do I not fully understand what it mean? Or does the statement only apply to types that implements the Copy trait? Can anyone explain what the problem is?

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions