Skip to content

Instantly share code, notes, and snippets.

@dacr
Created October 16, 2024 19:54

Revisions

  1. dacr created this gist Oct 16, 2024.
    42 changes: 42 additions & 0 deletions hello-enums.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/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);
    }