Generic Data Types
You can use generics to abstract over the concrete field type. Returning to the exercise for the previous segment:
Speaker Notes
This slide should take about 10 minutes.
- Q: Why
L
is specified twice inimpl<L: Logger> .. VerbosityFilter<L>
? Isn’t that redundant?- This is because it is a generic implementation section for generic type. They are independently generic.
- It means these methods are defined for any
L
. - It is possible to write
impl VerbosityFilter<StderrLogger> { .. }
.VerbosityFilter
is still generic and you can useVerbosityFilter<f64>
, but methods in this block will only be available forPoint<StderrLogger>
.
- Note that we don’t put a trait bound on the
VerbosityFilter
type itself. You can put bounds there as well, but generally in Rust we only put the trait bounds on the impl blocks.