Unlocking Rust’s Power: A Deep Dive into Associated Functions & Methods

Chidozie C. Okafor
3 min readApr 10, 2023

--

Rust is an expressive and powerful programming language known for its emphasis on safety, performance, and concurrency. Its ability to create associated functions and methods within structs and enums is one of its distinguishing features, allowing developers to write more organized and maintainable code. In this article, we will delve into these features in depth and provide examples of their application and benefits.

Rust’s Building Blocks: Structs and Enums

Before delving into related functions and methods, it’s critical to understand the fundamentals of structs and enums in Rust.

Structs are used to define custom data types with multiple fields, whereas enums define types with multiple distinct variants. Both structs and enums can have associated functions and methods that provide type-specific behavior.

Consider a box of brightly colored Lego bricks. You can use these bricks to construct various structures such as houses, cars, and even animals. In the programming language Rust, we also have building blocks known as structs and enums. They assist us in creating various types of things in our computer programs.

Consider associated functions and methods to be special instructions that assist you in building and playing with your Lego creations.

Associated Functions: The Static Sidekick

In other languages, associated functions are analogous to static methods. They are defined within the implementation block of a struct or enum, but do not accept a self reference as their first parameter. This means they can be called on the type itself rather than an instance of the type.

Associated functions are similar to the step-by-step instructions that come with a new Lego set. This guide will assist you in building a specific item, such as a house, without the use of any other pieces. Associated functions in Rust allow us to create new things without requiring an existing one.

For example, let’s say we want to make a balloon in our program. We can use the associated function “new” to create a balloon in any color we want:

// This is our balloon building block
struct Balloon {
color: String,
}

// Here are the instructions to create a new balloon
impl Balloon {
fn new(color: String) -> Balloon {
Balloon { color }
}
}

// Let's make a red balloon!
let red_balloon = Balloon::new("red".to_string());

Methods: Your Personal Instance Assistant

Methods are similar to associated functions in that their first parameter is a reference to self. This means they are called on a type instance rather than the type itself. Methods have access to instance data and can change it as needed.

Methods are similar to the enjoyable activities you can do with your Lego creations. Assume you’ve built a Lego car and now want to make it move or honk its horn. Methods in Rust allow us to do things with our existing creations.
Continuing with our balloon example, suppose we want to pop the balloon. We can use a technique called “pop” to make the balloon go “BOOM!” as shown below:

// This is our balloon building block
struct Balloon {
color: String,
}

// Here are the instructions to create a new balloon and make it pop
impl Balloon {
fn new(color: String) -> Balloon {
Balloon { color }
}

// This method makes the balloon pop
fn pop(&self) {
println!("{} balloon goes BOOM!", self.color);
}
}

// Let's make a red balloon and then make it pop!
let red_balloon = Balloon::new("red".to_string());
red_balloon.pop(); // The red balloon goes BOOM!

So, associated functions and methods in Rust are analogous to the special instructions and activities that assist us in building and playing with our Lego creations. They make programming more enjoyable and exciting, similar to playing with Legos!

Harnessing the Power of Rust

Rust’s associated functions and methods are powerful tools for developers who want to write clean, organized, and maintainable code. You can unlock the full potential of Rust and create more efficient and robust applications by understanding and leveraging these features.

Explore the world of Rust and harness the power of related functions and methods to improve your programming abilities and create exceptional software.

--

--

Chidozie C. Okafor
Chidozie C. Okafor

Written by Chidozie C. Okafor

Software Engineer & Backend Magician 🎩 | Python, Rust | TypeScript, Node.js | Golang | Kafka & GRPC

No responses yet