Rust Structs
Give related fields names instead of depending on tuple positions.
How it works
A struct groups fields into a named type. This makes a record’s meaning clearer when values share the same primitive type or appear in several functions. Field types prevent some mistakes, but two u32 fields can still be swapped logically; constructors and validation may be needed for stronger invariants. Visibility determines which callers can construct or mutate fields directly.
Run the code
Reviewed with Rust 1.97.0, edition 2024, using the standard library only. Save the complete download as lesson-name.rs in a separate learning folder, compile with rustc --edition=2024 lesson-name.rs and run the resulting program (lesson-name.exe on Windows). Use the actual lesson filename. The final project includes complete Cargo package files; run cargo test --offline and cargo run --offline from that separate package folder. There is no Rust browser runner; PHP, Apache and MySQL are not needed.
This complete example was executed in the stated local review environment.
Example
fn main() {
struct Tray { area: String, packets: u32 }
let tray = Tray { area: String::from("north"), packets: 8 };
assert_eq!(tray.packets, 8);
println!("{}: {} packets", tray.area, tray.packets);
}
Expected result
north: 8 packets
The area owns its text, so the record can outlive the expression that created it. Named fields explain the print arguments more clearly than a tuple of unrelated positions. A real package can keep fields private and expose a checked constructor if zero or oversized counts are invalid.
Primary technical reference: Rust documentation.