Rust
December 2, 2026 5 min read

Structs in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is a Struct?

A struct (structure) is a custom data type that allows us to group multiple related values under one name.

Each value inside a struct is called a field.

Example

struct Student {
name: String,
age: u32,
marks: f64,
}

fn main() {
let student = Student {
name: String::from("Hemant"),
age: 32,
marks: 85.5,
};

println!("Name: {}", student.name);
println!("Age: {}", student.age);
println!("Marks: {}", student.marks);
}

Output:

Name: Hemant
Age: 32
Marks: 85.5

Understanding the Code

  1. struct is the keyword used to define a structure.
  2. Student is the struct name.
  3. name, age, and marks are fields.
  4. String, u32, and f64 are field data types.
  5. Student { ... } creates an instance of the struct.
  6. student.name accesses the name field.

2. Struct Syntax

struct StructName {
field1: data_type,
field2: data_type,
field3: data_type,
}

Creating an instance:

let variable_name = StructName {
field1: value1,
field2: value2,
field3: value3,
};

Remember: When creating an instance, you must provide all required fields unless you use struct update syntax.

3. Accessing Struct Fields

We use dot notation (.) to access individual fields.

Example

struct Employee {
name: String,
salary: u32,
department: String,
}

fn main() {
let employee = Employee {
name: String::from("Rahul"),
salary: 25000,
department: String::from("IT"),
};

println!("Employee: {}", employee.name);
println!("Salary: {}", employee.salary);
println!("Department: {}", employee.department);
}

Output:

Employee: Rahul
Salary: 25000
Department: IT

4. Mutable Structs

By default, struct instances are immutable.

To modify their fields, declare the entire instance using mut.

Example

struct Student {
name: String,
marks: u32,
}

fn main() {
let mut student = Student {
name: String::from("Hemant"),
marks: 80,
};

student.marks = 95;

println!("Name: {}", student.name);
println!("Marks: {}", student.marks);
}

Output:

Name: Hemant
Marks: 95

Important: Rust does not allow you to declare only one struct field as mutable. Mutability is applied to the entire instance.

5. Creating Multiple Struct Instances

We can create multiple instances of the same struct.

Example

struct Student {
name: String,
age: u32,
marks: u32,
}

fn main() {
let student1 = Student {
name: String::from("Hemant"),
age: 32,
marks: 90,
};

let student2 = Student {
name: String::from("Rahul"),
age: 25,
marks: 85,
};

println!("Student 1: {}", student1.name);
println!("Student 2: {}", student2.name);
}

Output:

Student 1: Hemant
Student 2: Rahul

Both instances use the same structure but contain different values.

6. Struct Update Syntax

Sometimes we want to create a new struct instance using values from an existing instance.

Rust provides struct update syntax using ...

Example

struct Student {
name: String,
age: u32,
marks: u32,
}

fn main() {
let student1 = Student {
name: String::from("Hemant"),
age: 32,
marks: 90,
};

let student2 = Student {
name: String::from("Rahul"),
..student1
};

println!("Student 2 Name: {}", student2.name);
println!("Student 2 Age: {}", student2.age);
println!("Student 2 Marks: {}", student2.marks);
}

Output:

Student 2 Name: Rahul
Student 2 Age: 32
Student 2 Marks: 90

Understanding ..student1

It means:

  1. Use the explicitly provided values first.
  2. Take all remaining fields from student1.

Important: Struct update syntax moves or copies remaining field values according to their types. Because name is a String, it is moved from student1 into student2 in this example. Therefore, student1 can no longer be used as a complete struct.

7. Tuple Structs

A tuple struct is similar to a tuple, but it has a custom type name.

Unlike a regular struct, tuple structs do not use named fields.

Example

struct Color(u8, u8, u8);

fn main() {
let red = Color(255, 0, 0);

println!("Red: {}", red.0);
println!("Green: {}", red.1);
println!("Blue: {}", red.2);
}

Output:

Red: 255
Green: 0
Blue: 0

Here:

  1. Color is the tuple struct name.
  2. red is an instance.
  3. .0, .1, and .2 access the fields by position.

Another Example

struct Employee(String, u32, String);

fn main() {
let employee = Employee(
String::from("Rahul"),
25000,
String::from("IT"),
);

println!("Name: {}", employee.0);
println!("Salary: {}", employee.1);
println!("Department: {}", employee.2);
}

Output:

Name: Rahul
Salary: 25000
Department: IT

8. Unit-Like Structs

A unit-like struct is a struct without any fields.

Syntax

struct StructName;

Example

struct Admin;

fn main() {
let user = Admin;

println!("Admin user created!");
}

Output:

Admin user created!

Unit-like structs are useful when we need a custom type without storing any data.

9. Structs with Functions

We can create functions that accept struct instances as parameters.

Example

struct Student {
name: String,
marks: u32,
}

fn display_student(student: &Student) {
println!("Name: {}", student.name);
println!("Marks: {}", student.marks);
}

fn main() {
let student = Student {
name: String::from("Hemant"),
marks: 90,
};

display_student(&student);
}

Output:

Name: Hemant
Marks: 90

Here:

  1. &Student means the function borrows a student instance.
  2. The function can read its fields.
  3. Ownership remains with main().

10. Structs with Mutable References

We can pass a mutable reference to a function when we want that function to modify a struct.

Example

struct Student {
name: String,
marks: u32,
}

fn update_marks(student: &mut Student) {
student.marks += 5;
}

fn main() {
let mut student = Student {
name: String::from("Hemant"),
marks: 80,
};

update_marks(&mut student);

println!("Name: {}", student.name);
println!("Updated Marks: {}", student.marks);
}

Output:

Name: Hemant
Updated Marks: 85

Here:

  1. &mut Student accepts a mutable reference.
  2. student.marks += 5 modifies the original struct.
  3. Ownership is not transferred.

11. Deriving Debug for Structs

Normally, we cannot print a struct directly using {} or {:?} unless the appropriate formatting trait is implemented.

Rust provides #[derive(Debug)] to automatically implement the Debug trait for many simple structs.

Example

#[derive(Debug)]
struct Student {
name: String,
age: u32,
marks: f64,
}

fn main() {
let student = Student {
name: String::from("Hemant"),
age: 32,
marks: 90.5,
};

println!("{:?}", student);
}

Output:

Student { name: "Hemant", age: 32, marks: 90.5 }

Pretty Debug Formatting

Use {:#?} for a more readable, multi-line output.

println!("{:#?}", student);

Output:

Student {
name: "Hemant",
age: 32,
marks: 90.5,
}

12. Difference Between Struct and Tuple


Feature

Struct

Tuple

Fields

Named

Positional

Access

Dot notation with field name

Dot notation with index

Readability

More descriptive

Less descriptive for many values

Data types

Different types allowed

Different types allowed

Example

Student { name: "Hemant" }

("Hemant", 32)

13. Quick Reference


Concept

Syntax

Define struct

struct Student {}

Create instance

Student { name: String::from("Hemant") }

Access field

student.name

Mutable instance

let mut student = ...

Update field

student.marks = 90;

Struct update

..student1

Tuple struct

struct Color(u8, u8, u8);

Unit struct

struct Admin;

Debug output

#[derive(Debug)]

Borrow struct

&Student

Mutable borrow

&mut Student