Skip to content

Latest commit

 

History

History
265 lines (187 loc) · 7.91 KB

06_structs.md

File metadata and controls

265 lines (187 loc) · 7.91 KB

🦀 30 Days Of Rust: Day 6 - Structs 🚀

LinkedIn Follow me on GitHub

Author: Het Patel

October, 2024

<< Day 5 | Day 7 >>

30DaysOfRust


📘 Day 6 - Structs

👋 Welcome

Welcome to Day 6 of the 30 Days of Rust! 🎉 Today, we will explore Structs in Rust. Structs allow you to create custom data types that can group related values together. Let’s understand how they work and how you can use them effectively! 🚀

Congratulations! 🎉 You've taken the first step in your journey to master the 30 Days of Rust programming challenge. In this challenge, you will learn the fundamentals of Rust and how to harness its power to write efficient, fast, and safe code. By the end of this journey, you'll have gained a solid understanding of Rust's core concepts and best practices, helping you become a confident Rustacean. 🦀

Feel free to join the 30 Days of Rust community on Discord, where you can interact with others, ask questions, and share your progress!

🔍 Overview

In this lesson, you will learn:

  • How to define and create instances of structs.
  • Different types of structs: classic, tuple, and unit-like.
  • How to update structs and create methods associated with them.
  • Practical applications of structs through examples and exercises.

🛠 Environment Setup

Ensure that you have your Rust environment set up correctly from Day 1. If you haven’t installed Rust yet, please refer to the setup instructions from Day 1.

📖 Understanding Structs

🧩 What is a Struct?

A struct in Rust is a custom data type that allows you to name and package multiple related values together.

📌 Defining Structs

You can define a struct with named fields:

Example:

struct User {
    username: String,
    email: String,
    active: bool,
    sign_in_count: u64,
}

🔄 Creating Instances of Structs

To create an instance of a struct, specify the values for its fields:

Example:

fn main() {
    let user1 = User {
        username: String::from("Rustacean"),
        email: String::from("[email protected]"),
        active: true,
        sign_in_count: 1,
    };
    
    println!("Username: {}", user1.username);
}

Output:

Username: Rustacean

🛠 Using Structs in Functions

You can pass structs to functions or return them from functions.

Example:

fn main() {
    let user1 = create_user(String::from("[email protected]"), String::from("Rustacean"));
    println!("Email: {}", user1.email);
}

fn create_user(email: String, username: String) -> User {
    User {
        email,
        username,
        active: true,
        sign_in_count: 1,
    }
}

🔄 Updating Structs

Use struct update syntax to create a new instance from an existing one.

Example:

fn main() {
    let user1 = User {
        username: String::from("Rustacean"),
        email: String::from("[email protected]"),
        active: true,
        sign_in_count: 1,
    };
    
    let user2 = User {
        email: String::from("[email protected]"),
        ..user1
    };
    
    println!("Username: {}", user2.username);
}

Output:

Username: Rustacean

🔄 Tuple Structs

Tuple structs are similar to tuples, but they have a custom name and allow creating different types:

Example:

struct Color(i32, i32, i32);
fn main() {
    let black = Color(0, 0, 0);
    println!("Color: {}, {}, {}", black.0, black.1, black.2);
}

🛠 Unit-Like Structs

A struct without any fields is called a unit-like struct:

Example:

struct AlwaysEqual;
fn main() {
    let _subject = AlwaysEqual;
}

🛠 Structs with Methods

You can implement methods on structs using impl:

Example:

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

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn main() {
    let rect = Rectangle { width: 30, height: 50 };
    println!("Area: {}", rect.area());
}

Output:

Area: 1500

🎯 Hands-On Challenge

  1. Create a program that defines and uses a struct for a book with fields like title, author, pages, and publisher.
  2. Write functions to calculate and display book details using the struct.

💻 Exercises - Day 6

✅ Exercise: Level 1

  1. Define a struct for a rectangle and implement methods to calculate area and perimeter.
  2. Create a program to demonstrate the use of struct update syntax.

✅ Exercise: Level 2

  1. Write a program that uses a tuple struct to store RGB color values.
  2. Implement a method on a struct that compares two struct instances for equality.
  3. Write a function that returns an instance of a unit-like struct.

🎥 Helpful Video References

📝 Day 6 Summary

  • Learned about defining and using structs.
  • Explored different types of structs, including tuple and unit-like.
  • Understood how to implement methods and use structs in practical scenarios.

🌟 Great job on completing Day 6! Keep practicing, and get ready for Day 7 where we will explore Enums in Rust!

Thank you for joining Day 6 of the 30 Days of Rust challenge! If you found this helpful, don’t forget to Star GIF star this repository, share it with your friends, and stay tuned for more exciting lessons ahead!

Stay Connected
📧 Email: Hunterdii
🐦 Twitter: @HetPate94938685
🌐 Website: Working On It(Temporary)

<< Day 5 | Day 7 >>