Using struct update syntax to init struct passing fields of another struct]

struct A
{
  field1 : i32,
  field2 : i32
}

impl Default for A
{
  fn default() -> Self
  {
      Self
      {
        field1 : 0,
        field2 : 1
      }
  }
}

struct B
{
  field2 : i32
}

fn main()
{
  let b = B{ field2 : 2 };
  let a = A 
  { 
    field2 : b.field2,
    .. Default::default()
  };
}

Is there an expression that allows to pass fields of b into initialization of struct A without explicitly typing field names in case when both structs have common properties of the same type?

Like use:

let a = A 
{ 
  ..b,
  .. Default::default()
};

Instead of:

let a = A 
{ 
  field2 : b.field2,
  .. Default::default()
};

No.

1 Like

There's no such feature. You have to write it the long way. You could make a macro if you need to use this a lot.

1 Like