On 30/03/2014 09:50, Vladimir Pouzanov wrote:
I have a chunk of code that toggles different functions on hardware
pins, that looks like this:

   enum Function {
     GPIO = 0,
     F1 = 1,
     F2 = 2,
     F3 = 3,
   }
   fn set_mode(port: u8, pin: u8, fun: Function)

What I would like to have is an enum of human-readable values instead of
F1/F2/F3

[...]

let fun_idx: u8 = FUNCTIONS[port][pin][fun]


I don’t know if you can directly access the static strings for the variants’ names behind this, but you can use #[deriving(Show)] to get a string representation of any enum value:

For example:

    #[deriving(Show)]
    enum Function {
        GPIO = 0,
        F1 = 1,
        F2 = 2,
        F3 = 3,
    }

    fn main() {
        let f = GPIO;

        println!("{:?}", f)

        let s: ~str = format!("{:?}", f);
        println!("{}", s)
    }

Output:

    GPIO
    GPIO

--
Simon Sapin
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to