Double Double
Traits show up so much in Rust that we’ve already mentioned them multiple times in our previous lessons. It’s time to finally dive into this topic. We’ll start off with some simpler examples of traits. Then, we’ll get into some stronger motivation for using traits, focusing on some limitations we cleverly avoided with type parameters previously.
Let’s say I want to write a function that can double both i32
s and i64
s. I’m going to start off by using the ugly “add the type to the name” approach and end up with two functions:
Get hands-on with 1400+ tech skills courses.