Generic Data Types

You can use generics to abstract over the concrete field type. Returning to the exercise for the previous segment:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Speaker Notes

This slide should take about 10 minutes.
  • Q: Why L is specified twice in impl<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 use VerbosityFilter<f64>, but methods in this block will only be available for Point<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.