rust-lang / rust-lang/book

[Moving Values] Accessing fields of a borrowed struct instance

Open
#4,402 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
18.3k
Forks
4.1k
Avg merge
14m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Chapter 5 section, then reproduce the update_user example using the borrowed struct and String field described in the issue. Compare the field-access behavior for the documented scalar example and the String case; done means the chapter clearly explains the scope and limitations of the statement.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.