title: Conveniently updating a struct's fields using a struct with a subset of the original struct's fields (Data transfer objects in Rust)
body: <p>I'm currently trying to find out if there's a convenient way in Rust to update the fields of a struct A with a struct B, where the fields of struct B are basically a subset of the fields of struct A.</p>
  <p>In practice I'd like to use an <code>UpdateUserDto</code> struct (used for JSON deserialization) to update the fields of a <code>User</code> struct, so I can save the updated <code>User</code> struct in a database.</p>
  <p>Example:</p>
  <pre class="lang-rs prettyprint-override"><code>#[derive(Debug, Clone, Serialize, Queryable, Identifiable, AsChangeset)]
  #[serde(rename_all = &quot;camelCase&quot;)]
  pub struct User {
      pub id: Uuid,
      pub email: String,
      pub name: String,
  }
  
  #[derive(Debug, Clone, Deserialize)]
  pub struct UpdateUserDto{
      pub email: String,
      pub name: String,
  }
  
  ...
  
  // Current user from the database.
  let mut user: User = db::users::get_by_id(...);
  
  // Deserialized JSON data.
  let update_user_data: UpdateUserData = ...;
  
  // These are the cumbersome operations that I'd like to avoid.
  user.email = update_user_dto.email;
  user.name = update_user_dto.name;
  
  // Saves the updated user in the database.
  db::users::update(..., &amp;user);
  </code></pre>
  <p>It doesn't seem to be the sort of operation that one usually does in Rust, since I couldn't find anything like this anywhere on the internet. But it's quite possible that I just haven't figured out what and where to look for...</p>