Created
October 16, 2024 19:54
-
-
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/cbea0d0abf93b9548d99d96d054be8a53cefb2c6
This file contains 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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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