How to Make Struct Fields Public vs Private in Rust

Make Rust struct fields public by adding the `pub` keyword before the field name, as fields are private by default.

Struct fields are private by default in Rust; add the pub keyword before a field name to make it public. Use pub on specific fields to expose them while keeping others private, or on the struct itself to make the type accessible.

struct User {
    pub username: String,
    email: String,
}

fn main() {
    let user = User {
        username: String::from("alice"),
        email: String::from("alice@example.com"),
    };
    println!("{}", user.username);
}