Member-only story
Generics
The days are flying by, it’s already day 22! Yesterday, we discussed Traits, and if you missed it, you can catch up with the link below.
Today, we’re building on what we learnt yesterday with Generics, as we will see below. So read on.
Rust’s generics let you write code that works with multiple types without sacrificing performance or safety.
Think of generics as a way to create a blueprint for your functions, structs, or traits, where the specific type is filled in later.
This is super useful when you want to avoid rewriting the same logic for different data types, like i32, f64, or a custom struct.
Why Generics?
What kind of problems are genetics trying to solve?
Imagine a scenario where you’re building a small inventory system for an e-commerce app.
You need a function to calculate the total value of items in stock, but the items could be books (priced with integers), electronics (priced with floating-point numbers), or even custom types like discount vouchers.

