Skip to content

Explain struct partial initiation/update syntax in more detail #315

Closed
@jeekobu

Description

@jeekobu

When reading about struct update syntax in The Book 2nd Edition, I wondered what would happen with Copy field members versus Clone field members versus neither. Googling around didn't help (if there's an RFC I couldn't find it) and eventually I just used the playground to figure out the behavior: Copy fields are copied, other fields are moved, possibly leading to a "partially moved" struct.

It would be nice if the book and/or reference spelled this out.

Citations:
https://doc.rust-lang.org/reference/expressions/struct-expr.html
"with [...] the values in the base expression for all other fields", 'with' is vague

https://doc.rust-lang.org/stable/book/second-edition/ch05-01-defining-structs.html
"use the rest of the values from the fields of the instance", 'use' is vague

https://doc.rust-lang.org/nightly/book/first-edition/structs.html#update-syntax
"it will copy the values", technically not true for non-Copy types

https://play.rust-lang.org/?gist=cb23fb3f9f9c631370b2b631367b8b16&version=stable
Example of a "partially moved value"

Metadata

Metadata

Assignees

Labels

New ContentMissing features or aspects of language not currently documented.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions