Associated Types
We'll cover the following...
I want to calculate how far my car will travel in 3 hours if it’s going at a certain speed. I live in a country that uses the metric system, so I’m going to define two helper structs, Kmh (kilometers per hour) and Km (kilometers).
Press + to interact
#[derive(Debug, Clone, Copy)]struct Kmh {value: u32}#[derive(Debug, Clone, Copy)]struct Km {value: u32}
Defining a struct with a single field like this is a great way to protect against programmer error. Now it’s impossible to accidentally compare Kmh (a unit of speed) against Km (a unit of distance). In any event, I can now write a method on Kmh to tell me how far my car has ...
Ask