The issue is that only the color
variant of Colors
actually has the r
, g
and b
fields. Consider the scenario where you add an extra variant:
enum Colors {
// Enum variants should be capitalized!
Color { r:u8, g:u8, b:u8 },
Hsv { h: u8, s: u8, v: u8 },
}
What happens if you call return_values
on a Colors::Hsv
? That variant doesn't have the rgb
fields, so your method no longer makes sense!
In order to make this work, you need to check which kind of Colors
your instance of self
is in the method:
fn return_values(&self) -> (u8, u8, u8) {
match self {
Colors::Color { r, g, b } => (r, g, b),
Colors::Hsv { h, s, v } => (h, s, v),
}
}
Now you're being explicit about where r
, g
and b
come from, so everything should work!
Note however, that if you don't intent to add extra kinds of Colors
, you're not really gaining anything by making it an enum - you could just use a struct like this:
struct Color {
r: u8,
g: u8,
b: u8,
}
Your original impl
would work fine on that struct, because every instance of it has the r
, g
and b
fields.