- 📘 Day 6 - Structs
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!
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.
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.
A struct in Rust is a custom data type that allows you to name and package multiple related values together.
You can define a struct with named fields:
Example:
struct User {
username: String,
email: String,
active: bool,
sign_in_count: u64,
}
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
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,
}
}
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 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);
}
A struct without any fields is called a unit-like struct:
Example:
struct AlwaysEqual;
fn main() {
let _subject = AlwaysEqual;
}
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
- Create a program that defines and uses a struct for a book with fields like
title
,author
,pages
, andpublisher
. - Write functions to calculate and display book details using the struct.
- Define a struct for a rectangle and implement methods to calculate area and perimeter.
- Create a program to demonstrate the use of struct update syntax.
- Write a program that uses a tuple struct to store RGB color values.
- Implement a method on a struct that compares two struct instances for equality.
- Write a function that returns an instance of a unit-like struct.
- Rust Structs and Enums(Language : Hindi)
- Rust Structs and Enums(Language : English)
- Methods in Rust
- Structs in Rust Explained
- 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 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)