Sitemap
Rustaceans

Exploring Rust: Insights and Expertise from Rustaceans

Generics

5 min readJul 22, 2025

--

Press enter or click to view image in full size
Image made by Excalidraw

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.

--

--

Rustaceans
Rustaceans

Published in Rustaceans

Exploring Rust: Insights and Expertise from Rustaceans

John Philip
John Philip

Written by John Philip

Dev | Life Café | Twitter at @amjohnphilip | Email: dxphilo@gmail.com

Responses (1)