A data model can normalize raw input at the boundary so later code uses consistent fields.

Program

Play the program to choose a raw name and build a normalized user record.

raw_name
normalized_user_record.rs
Replay: real traced execution (multi-file project)
struct User {
    name: String,
    role: String,
}

fn main() {
    let raw_name = " Ada ";
    let raw_role = "ADMIN";
    let user = User {
        name: raw_name.trim().to_string(),
        role: raw_role.to_lowercase(),
    };
    println!("{}:{}", user.name, user.role);
}
struct User {
    name: String,
    role: String,
}

fn main() {
    let raw_name = " Grace ";
    let raw_role = "ADMIN";
    let user = User {
        name: raw_name.trim().to_string(),
        role: raw_role.to_lowercase(),
    };
    println!("{}:{}", user.name, user.role);
}
struct User {
    name: String,
    role: String,
}

fn main() {
    let raw_name = " Linus ";
    let raw_role = "ADMIN";
    let user = User {
        name: raw_name.trim().to_string(),
        role: raw_role.to_lowercase(),
    };
    println!("{}:{}", user.name, user.role);
}
  1. raw_name ← " Ada ", raw_role ← "ADMIN", user ← (empty)

    6fn main() {7    let raw_nam→ " Ada "e = " Ada "; //@raw_name=" Ada ", " Grace ", " Linus "8    let raw_rol→ "ADMIN"e = "ADMIN";9    let use→ (empty)r = User {10        name: raw_name.trim().to_string(),11        role: raw_role.to_lowercase(),12    };13    println!("{}:{}", user.name, user.role);14}
    outputAda:admin
  1. raw_name ← " Grace ", raw_role ← "ADMIN", user ← (empty)

    6fn main() {7    let raw_nam→ " Grace "e = " Grace ";8    let raw_rol→ "ADMIN"e = "ADMIN";9    let use→ (empty)r = User {10        name: raw_name.trim().to_string(),11        role: raw_role.to_lowercase(),12    };13    println!("{}:{}", user.name, user.role);14}
    outputGrace:admin
  1. raw_name ← " Linus ", raw_role ← "ADMIN", user ← (empty)

    6fn main() {7    let raw_nam→ " Linus "e = " Linus ";8    let raw_rol→ "ADMIN"e = "ADMIN";9    let use→ (empty)r = User {10        name: raw_name.trim().to_string(),11        role: raw_role.to_lowercase(),12    };13    println!("{}:{}", user.name, user.role);14}
    outputLinus:admin
struct `User` names the fields that belong together in one record.
normalization `trim` and `to_lowercase` clean raw input before it is stored.
boundary After construction, callers can use `user.name` and `user.role` without re-cleaning.