Question about 'struct update syntax'

I'm learning Rust. I have a puzzle that I want to be answered:

/// struct User
struct User {
    id: usize,
    name: String,
    age: u8,
fn main() {
    let old_user = User {
        id: 1001,
        name: String::from("allen"),
        age: 23,
    let new_user = User {
        id: 1002,
    // it will give a error because 'name' has been removed
    println!("{:?}", old_user);
    // but it works

why can this be?

The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
src: Reference: functional update syntax


This is actually a kind of new feature. It's a Partial move

1 Like

Actually this link is a bit better of an explanation

1 Like

In case it wasn't clear from the other comments, partial moves aren't a struct update specific thing. The other thing to note is that struct update works like this, and not like this.


yes, I understand how it works.
and the article given by abdavis also clearly illustrates this point.

I just had a simple and crude idea:
either clone the ‘name’ to new_user, either deprecated the old_user entirely. :rofl:

yes. it explains why



The final conclusion is: i can use 'old_user.xx' but i can't use 'old_user'.
It confuses me as to whether I should use 'old_user' anymore

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.