Struct Basics
In this page:
struct Name {
field1: Type1,
field2: Type2,
}
let value = Name { field1: v1, field2: v2 };
value.field1
Defining a Struct
The struct keyword defines a new custom type with named fields, each with its own type. This groups related data together under one meaningful name instead of using separate loose variables.
Example: Defining a Struct
// Define a struct `User` to group related fields together
struct User {
name: String,
age: u32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `user`, set to `User { name: String::from("Ada"), age: 30 }`
let user = User { name: String::from("Ada"), age: 30 };
// Print "{} is {} years old" to the console, substituting in `user.name, user.age` (with a trailing newline)
println!("{} is {} years old", user.name, user.age);
}
Login to try C/C++/Java/PHP code in the editor
Creating Struct Instances
You create an instance of a struct with a struct literal: the type name followed by curly braces listing a value for every field. All fields must be provided.
Example: Creating Struct Instances
// Define a struct `Point` to group related fields together
struct Point {
x: f64,
y: f64,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `origin`, set to `Point { x: 0.0, y: 0.0 }`
let origin = Point { x: 0.0, y: 0.0 };
// Print "({}, {})" to the console, substituting in `origin.x, origin.y` (with a trailing newline)
println!("({}, {})", origin.x, origin.y);
}
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying Fields
Fields are read using dot notation. If the struct instance is bound with mut, its fields can also be reassigned individually after creation.
Example: Accessing and Modifying Fields
// Define a struct `Counter` to group related fields together
struct Counter {
value: i32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `counter`, initialized to `Counter { value: 0 }`
let mut counter = Counter { value: 0 };
counter.value += 1;
counter.value += 1;
// Print "Counter is at {}" to the console, substituting in `counter.value` (with a trailing newline)
println!("Counter is at {}", counter.value);
}
Login to try C/C++/Java/PHP code in the editor
Field Init Shorthand
When a variable's name matches a struct field's name, you can omit repeating the name, writing just the variable instead of field: field.
Example: Field Init Shorthand
// Define a struct `Point3D` to group related fields together
struct Point3D {
x: i32,
y: i32,
z: i32,
}
// Define the function `make_point` taking `x`, `y`, `z` and returning a `Point3D`
fn make_point(x: i32, y: i32, z: i32) -> Point3D {
Point3D { x, y, z }
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `p`, set to `make_point(1, 2, 3)`
let p = make_point(1, 2, 3);
// Print "({}, {}, {})" to the console, substituting in `p.x, p.y, p.z` (with a trailing newline)
println!("({}, {}, {})", p.x, p.y, p.z);
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting a field when constructing a struct literal, which the compiler rejects since every field is required.
- Trying to make just one field of a struct instance mutable -- mutability applies to the whole variable binding, not per field.
- Confusing struct field access
instance.fieldwith function calls, forgetting there is no parentheses for plain field access.
- A struct groups related named fields together under one custom type, defined with the
structkeyword. - Struct instances are created with a struct literal listing every field's value.
- Fields are accessed with dot notation, e.g.
instance.field. - Marking an instance variable
mutallows changing any of its fields afterward.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: