Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Structs | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 13 of 50

·rust

Structs

15 minutes

Course progress26%

Structs

Structs (structures) group related data together. They're like tuples but with named fields, making code more readable and maintainable.

Defining Structs

struct Point {
    x: f64,
    y: f64,
}

struct Person {
    name: String,
    age: u32,
    email: ,
}
String

Creating Struct Instances

fn main() {
    let p = Point { x: 1.0, y: 2.0 };
    println!(\"Point: ({}, {})\", p.x, p.y);
    
    let person = Person {
        name: String::from(\"Alice\"),
        age: 30,
        email: String::from(\"alice@example.com\"),
    };
    println!(\"Person: {}, age {}\", person.name, person.age);
}

Struct Field Init Shorthand

fn create_person(name: String, age: u32) -> Person {
    Person {
        name,    // Equivalent to name: name
        age,     // Equivalent to age: age
        email: String::from(\"no-reply@example.com\"),
    }
}

Struct Update Syntax

Create a new struct from an existing one:

let person1 = Person {
    name: String::from(\"Alice\"),
    age: 30,
    email: String::from(\"alice@example.com\"),
};

let person2 = Person {
    name: String::from(\"Bob\"),
    ..person1  // Copy remaining fields from person1
};

Tuple Structs

Structs without named fields:

struct Color(u8, u8, u8);
struct Point(f64, f64);

let black = Color(0, 0, 0);
let origin = Point(0.0, 0.0);

Unit-Like Structs

Structs without any fields:

struct Marker;

Methods on Structs

Define functions associated with structs using impl:

impl Point {
    fn distance_from_origin(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }
    
    fn move_by(&mut self, dx: f64, dy: f64) {
        self.x += dx;
        self.y += dy;
    }
    
    fn new(x: f64, y: f64) -> Point {
        Point { x, y }
    }
}

fn main() {
    let mut p = Point::new(3.0, 4.0);
    println!(\"Distance: {}\", p.distance_from_origin());  // 5.0
    p.move_by(1.0, 1.0);
}

Associated Functions

Functions called with :: syntax (like Point::new):

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

impl User {
    pub fn new(username: String, email: String) -> User {
        User { username, email }
    }
    
    pub fn display(&self) {
        println!(\"User: {}, Email: {}\", self.username, self.email);
    }
}

Debugging Structs

struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect = Rectangle { width: 30, height: 50 };
    println!(\"Rectangle: {:?}\", rect);  // ERROR: doesn't implement Debug
}

Add #[derive(Debug)] to enable debug printing:

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect = Rectangle { width: 30, height: 50 };
    println!(\"Rectangle: {:?}\", rect);
}
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonStringsContinue to lesson 14EnumsBack to Rust Fundamentals curriculum