ProgramGuru

Structs and named fields

Structs and named fields

A student has a name and a mark that belong together. A struct defines a type with named fields, making that relationship explicit. Named fields are easier to review than remembering which tuple position represents which fact.

What you’ll learn

  • Group student fields into a meaningful named type.
  • Separate field types from domain validation.

Structs and named fields in action

  1. Record: Diya, 72

    The constructor stores an owned name and an unsigned mark.

    Record: Diya, 72
  2. Update score field

    The mutable binding permits increasing its score to 80.

    Update score field
  3. Record: Diya, 80

    Formatting reads the fields without moving the name out, so the whole record remains usable.

    Record: Diya, 80
The constructor stores an owned name and an unsigned mark. The mutable binding permits increasing its score to 80. Formatting reads the fields without moving the name out, so the whole record remains usable.

Structs and named fields explained

Define the shape with struct, then construct a value by supplying its fields. Dot notation accesses a field. A struct containing String owns that String unless you deliberately design it around references. A mutable struct binding permits changing its fields, subject to their visibility and borrowing rules.

Connect this idea with methods and associated functions and result and recoverable errors.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

struct Student { name: String, score: u32 }
fn main() {
  let mut student = Student { name: String::from("Diya"), score: 72 };
  student.score += 8;
  println!("{}: {}", student.name, student.score);
}

Expected output

Diya: 80

Follow the execution

The constructor stores an owned name and an unsigned mark. The mutable binding permits increasing its score to 80. Formatting reads the fields without moving the name out, so the whole record remains usable.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: What advantage do named fields have over an unexplained (String, u32) tuple?

Rust 2024 · complete program

struct Student { name: String, score: u32 }
fn main() {
  let mut student = Student { name: String::from("Diya"), score: 72 };
  student.score += 8;
  println!("{}: {}", student.name, student.score);
}

Worked answer

They identify the meaning of each value. Student.name and Student.score make the record’s intended structure explicit instead of relying only on positional knowledge.

Mistakes and limits to recognize

An unsigned integer excludes negative marks but still permits values above 100. Struct construction is not automatically domain validation. Moving an owned field out can partially move a record, limiting later use of the whole record.

Check your understanding

What advantage do named fields have over an unexplained (String, u32) tuple?
Choose one answer. You can try again.

Answer and explanation

They identify the meaning of each value.

They identify the meaning of each value. Student.name and Student.score make the record’s intended structure explicit instead of relying only on positional knowledge.

Take the idea into your next program

They identify the meaning of each value. Student.name and Student.score make the record’s intended structure explicit instead of relying only on positional knowledge. An unsigned integer excludes negative marks but still permits values above 100.

Further reading: the official Rust Book on structs and named fields.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Methods and associated functions →