Day -8: Dive into Structs and Methods in Rust.

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Structs are a fundamental feature in Rust that allows you to define custom data types with their own fields and behaviors. Combining structs with methods provides an elegant way to encapsulate data and associated functionality within a single unit. In this article, we will dive into structs and methods in Rust, exploring how they work together to create powerful and reusable code.
A struct is a way to define a custom data type that groups related data fields together. Let's start by understanding how to define and use structs in Rust.
struct Person {
name: String,
age: u32,
}
fn main() {
let person1 = Person {
name: String::from("Alice"),
age: 25,
};
println!("Name: {}", person1.name);
println!("Age: {}", person1.age);
}
In the example above, we define a Person struct with two fields: name of type String and age of type u32. We create an instance of the struct called person1 and access its fields using dot notation.
Methods are functions associated with a particular struct or enum. They allow you to define behavior specific to instances of a struct or enum. Methods are defined within an impl block associated with the struct or enum.
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn is_square(&self) -> bool {
self.width == self.height
}
}
fn main() {
let rect = Rectangle {
width: 10,
height: 20,
};
println!("Area: {}", rect.area());
println!("Is Square? {}", rect.is_square());
}
In this example, we define a Rectangle struct with width and height fields. Within the impl block, we define two methods: area() and is_square(). The &self parameter represents an immutable reference to the struct instance.
The area() method calculates the area of the rectangle by multiplying its width and height. The is_square() method checks if the rectangle is a square by comparing its width and height.
Using methods allows us to encapsulate functionality related to a struct, promoting code reuse and improving readability.
In addition to methods, Rust allows associated functions that are called on the struct or enum itself, rather than on instances. Associated functions are defined within the impl block without the self parameter.
struct Circle {
radius: f64,
}
impl Circle {
fn new(radius: f64) -> Circle {
Circle { radius }
}
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
fn main() {
let circle = Circle::new(3.0);
println!("Area: {}", circle.area());
}
In this example, we define an associated function new() that constructs a new instance of the Circle struct. We call this function using the syntax Circle::new() without having to create a struct instance.
Structs and methods are powerful constructs in Rust that enable you to define custom data types with associated behavior. Structs allow you to group related data fields, while methods provide a way to define behavior specific to those structs. Using methods and associated functions enhances code organization, reusability, and encapsulation.
By leveraging structs and methods effectively, you can write clean, modular, and expressive code in Rust. Experiment with different struct designs and explore more advanced features of methods to unlock the full potential of Rust's capabilities. Happy coding in Rust!
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.