Share Behavior Through and Through

techno bossa nova, bubblegum dance chanson · 3:19

Listen on 93

Lyrics

[Verse 1]
When you want to share behavior across different types
Traits are the answer, they make your code right
Like a contract that says what methods you must define
Common functionality in a clean design

[Chorus]
Traits define what things can do
Share behavior through and through
Implement the methods true
Traits make Rust dreams come true
Define it once, use it twice
Common behavior, that's so nice

[Verse 2]
Say you have a Dog and Cat, both different struct types
But they both can make a sound, that's where traits provide
Create a trait called MakeSound with a method called speak
Now both pets can use this trait, the code's no longer weak

[Chorus]
Traits define what things can do
Share behavior through and through
Implement the methods true
Traits make Rust dreams come true
Define it once, use it twice
Common behavior, that's so nice

[Bridge]
Standard library traits are everywhere you look
Display for printing out, Debug for taking a look
Clone for making copies, Drop for cleanup time
Iterator for looping through, they work every time

[Verse 3]
Write your trait with pub keyword if you want to share
Add your method signatures with the types you declare
Then implement for each struct that needs this trait
The compiler checks your work, no room for mistake

[Chorus]
Traits define what things can do
Share behavior through and through
Implement the methods true
Traits make Rust dreams come true
Define it once, use it twice
Common behavior, that's so nice

[Outro]
Polymorphism made simple in the Rust way
Traits bring order to your code every single day

← One Size Fits All | Chain Your Fate →