Rise In Logo

Rust Programming for Smart Contract Development

Implementation of Generics using structs and enums in Rust

Generics with Structs

Generics will make your code more flexible, and it's going to feel like a walk in the park once you get the hang of it. So, let's dive right in!

Defining generic structs and their usage

Generic structs are like regular structs, but they have a secret power - they can work with different types without the need for you to write separate structs for each type. Mind-blowing, right? Instead of specifying a concrete type, we use a type parameter, which we'll replace with the actual type when we create an instance of the struct. 

This way, we get to reuse the same struct for multiple types, making our code DRY (Don't Repeat Yourself) and our lives easier.

To define a generic struct, just wrap your type parameter in angle brackets (<>) and place it right after the struct keyword. Here's an example:

struct Container<T> {
    value: T,

In this example, we've created a Container struct with a generic type parameter T. The value field inside the struct will hold an item of type T. This means that our Container can store any type we want, depending on how we instantiate it. Talk about versatility!

Demonstrating examples of generic structs with different types

Now that we've got our generic Container struct, let's see it in action with different types. Watch how easily it adapts to our whims!

// Using the Container struct with an i32 type
let int_container = Container { value: 42 };
println!("The answer to life, the universe, and everything: {}", int_container.value);

// Using the Container struct with a &str type
let str_container = Container { value: "Rustacean" };
println!("I'm a proud {}: {}", str_container.value, str_container.value);

// Using the Container struct with a f64 type
let float_container = Container { value: 3.14 };
println!("Approximation of Pi: {}", float_container.value);

As you can see, our generic Container struct is like a chameleon, gracefully adapting to different types without breaking a sweat. With generics, our structs have leveled up, and our code is more flexible and reusable than ever before. High five! 🙌

Generics with Enums

Alright, folks! It's time to bring some magic into the world of Rust enums. That's right, we're gonna make 'em generic! Trust me, it's gonna be awesome. So, buckle up, and let's dive into this fantastic world of generic enums.

Defining Generic Enums and Their Usage

First off, let me introduce you to the concept of generic enums. Just like we have structs and functions that can work with various types, we can also create enums that can handle multiple types. This means we can define an enum once and use it with different types, which saves us from writing repetitive code. Isn't that cool? Now, let me show you how it's done.

To create a generic enum, we simply use the angle brackets (<>) to define the generic type parameters. Just like a magician waving their wand, we can make our enums magically adapt to different types. Here's a simple example:

enum MagicalResult<T, E> {

In this example, we've created a generic MagicalResult enum that can handle two types: T for the success case and E for the failure case. By doing this, we've made our enum super flexible and easy to reuse.

Demonstrating Examples of Generic Enums with Different Types

Now, let's see our MagicalResult enum in action, shall we? Prepare to be amazed by the magic of Rust generics!

// Using MagicalResult with different types
let success_result: MagicalResult<i32, String> = MagicalResult::Success(42);
let failure_result: MagicalResult<i32, String> = MagicalResult::Failure("Oops, something went wrong!".to_string());

match success_result {
    MagicalResult::Success(value) => println!("We've got the answer: {}", value),
    MagicalResult::Failure(error) => println!("Error: {}", error),

match failure_result {
    MagicalResult::Success(value) => println!("We've got the answer: {}", value),
    MagicalResult::Failure(error) => println!("Error: {}", error),

As you can see, our MagicalResult enum works like a charm with both i32 and String types. We were able to use the same enum definition for different types without any extra effort. How awesome is that?

Advanced Generics Usage

Alright, buckle up, Rustaceans! We're about to dive into the wild world of advanced generics usage in Rust. Don't worry, we'll make it as fun and engaging as possible. So, let's get started!

Working with Multiple Generic Type Parameters

Sometimes, your generic functions or data structures may require more than one type parameter. Rust has your back! You can use multiple generic type parameters in your structs, enums, or functions by simply separating them with commas inside the angle brackets. Sounds easy, right? Let's see an example:

struct FancyPair<T, U> {
    first: T,
    second: U,

fn main() {
    let int_str_pair = FancyPair {
        first: 42,
        second: "The answer to life, the universe, and everything",

In this example, we've created a struct FancyPair with two generic type parameters, T and U. Now, you can create instances of FancyPair with different types for first and second. It's like having a dynamic duo of types!

Exploring Associated Types and Their Usage in Trait Definitions

We've already seen trait bounds on generic types, but Rust offers even more flexibility with associated types. Associated types allow you to associate a specific type with a trait, making it possible to use that type within the trait's methods. This adds an extra layer of awesomeness to your traits! Let's take a look at an example:

trait Vehicle {
    type Fuel;

    fn refuel(&mut self, fuel: Self::Fuel);

struct ElectricCar {
    battery_level: u32,

struct GasCar {
    gas_level: u32,

impl Vehicle for ElectricCar {
    type Fuel = u32;

    fn refuel(&mut self, charge: Self::Fuel) {
        self.battery_level += charge;
        println!("Battery charged to {}%", self.battery_level);

impl Vehicle for GasCar {
    type Fuel = f32;

    fn refuel(&mut self, gas: Self::Fuel) {
        self.gas_level += (gas * 100.0) as u32;
        println!("Gas tank filled to {}%", self.gas_level);

fn main() {
    let mut tesla = ElectricCar { battery_level: 50 };
    let mut ford = GasCar { gas_level: 40 };


In this example, we've created a Vehicle trait with an associated type called Fuel. This allows us to have different fuel types for different vehicle implementations. We then implement the Vehicle trait for two structs, ElectricCar and GasCar, with different associated Fuel types. 

Finally, we create instances of these structs and refuel them with their respective fuel types.

So, there you have it! Advanced generics usage in Rust can make your code more flexible, reusable, and, dare we say, fancier. With multiple generic type parameters and associated types, you're ready to tackle even the most complex type-related challenges in your Rust journey. Happy coding!


Rise In Logo

Rise together in web3