Last active
February 3, 2026 20:23
-
-
Save dacr/b01e205b941cbfea52251caec1ddb28d to your computer and use it in GitHub Desktop.
hello rust enums / published by https://github.com/dacr/code-examples-manager #40b68742-18a7-47f6-aaa5-817b22013b9e/cf01bcf64d66c6e709735e20a8670596f42710e2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env rust-script | |
| // summary : hello rust enums | |
| // keywords : rust, enums, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 40b68742-18a7-47f6-aaa5-817b22013b9e | |
| // created-on : 2024-10-16T08:54:21+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : ./$file | |
| #[derive(Debug)] | |
| enum Color { | |
| Red, | |
| RGB(u8, u8, u8), | |
| HSV(u8, u8, u8), | |
| Hex(String), | |
| } | |
| impl Color { | |
| fn print(&self) { | |
| match self { | |
| Color::Red => println!("Red"), | |
| Color::RGB(_, 0, 0) => println!("Some RED"), | |
| Color::RGB(r, g, b) => println!("RGB({r}, {g}, {b})"), | |
| Color::HSV(h, s, v) => println!("HSV({h}, {s}, {v})"), | |
| Color::Hex(hex) => println!("Hex({hex})"), | |
| } | |
| } | |
| } | |
| fn main() { | |
| let r1 = Color::RGB(255, 0, 0); | |
| let r2 = Color::Hex(String::from("FF0000")); | |
| r1.print(); | |
| r2.print(); | |
| println!("r1 = {:?}", r1); | |
| println!("r2 = {:?}", r2); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment