On 30/03/14 20:12, Simon Sapin wrote:
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
Note that {:?} is an reflection-based formatter that works with every
type (although has various problems, like being very slow), not the Show
one. One should just use {} to use the Show impl, i.e. in your example:
println!("{}", f)
Huon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev