Module show_notes::e008
source · Expand description
Just like something else
- Date: 2015-12-27
- Subject: Generics, traits, and shared behavior in Rust.
- Audio
§Notes
In this episode we cover—at a very high level—two more fundamental concepts in Rust programming: generics and traits.
Generics gives us the ability to write types and functions which can be used with more than one type. Traits give us the ability to specify behavior which can be implemented for more than one type. The combination gives us powerful tools for higher-level programming constructs in Rust.
§Comments on source code
Now that we have a handle on how tests work, we’ll use them to validate the behavior of our code going forward. This is great: we can show that the tests do what we think.
To today’s point, though: we actually know even apart from whether the tests run successfully that these generic functions and the associated traits are behaving as we want. Failure with generics is a compile-time error, not a runtime error.
§Links
- Rust Book
- Rust by Example
- Generics and traits in use in Diesel
§Sponsors
- Chris Palmer
- Derek Morr
- Luca Schmid
- Micael Bergeron
- Ralph Giles (“rillian”)
- reddraggone9
- William Roe
§Become a sponsor
§Follow
- New Rustacean:
- Twitter: @newrustacean
- App.net: @newrustacean
- Email: hello@newrustacean.com
- Twitter: @newrustacean
- Chris Krycho
- Twitter: @chriskrycho
- App.net: @chriskrycho
- Twitter: @chriskrycho
Structs§
- This is just a container which can hold any two types.
- A simple type to illustrate trait implementation.
Enums§
- This is
Option<T>
, but using Haskell’s names instead.
Functions§
- Demonstrate a function generic over any type.
- Demonstrate a function with a trait bound on a generic.