Enum values can contain futher values. Use pattern matching to access.
This commit is contained in:
parent
092e5693b7
commit
d5427c96ca
26
src/main.rs
26
src/main.rs
@ -1,21 +1,21 @@
|
||||
enum ColorSpace {
|
||||
RGB,
|
||||
RGBA,
|
||||
CMYK,
|
||||
YUV,
|
||||
enum Color {
|
||||
RGB(f32, f32, f32),
|
||||
RGBA(f32, f32, f32, f32),
|
||||
CMYK(f32, f32, f32, f32),
|
||||
YUV(f32, f32, f32),
|
||||
}
|
||||
|
||||
fn color_space_to_string(color_space: ColorSpace) -> String {
|
||||
match color_space {
|
||||
ColorSpace::RGB => String::from("RGB"),
|
||||
ColorSpace::RGBA => String::from("RGBA"),
|
||||
ColorSpace::CMYK => String::from("CMYK"),
|
||||
ColorSpace::YUV => String::from("YUV"),
|
||||
fn color_to_string(color: Color) -> String {
|
||||
match color {
|
||||
Color::RGB(red, green, blue) => String::from("RGB"),
|
||||
Color::RGBA(red, green, blue, alpha) => String::from("RGBA"),
|
||||
Color::CMYK(cyan, magenta, yellow, key) => String::from("CMYK"),
|
||||
Color::YUV(y, u, v) => String::from("YUV"),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let color_space = ColorSpace::RGB;
|
||||
let color = Color::RGB(1.0, 0.0, 0.0);
|
||||
|
||||
println!("Color Space: {}", color_space_to_string(color_space));
|
||||
println!("Color: {}", color_to_string(color));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user