Hi, I am trying to create a generic function to return value depending on the enum passed. But I don't know to create a generic trait for enum.
In following example, print_value works but I don't know how I can write a generic get_value function to get value from enum. #[deriving(Show)] enum MyTypes{ MyBool(bool), MyStr(String), MyInt(int) } fn print_value(arg: MyTypes){ match arg{ MyBool(x) => println!("Bool: {}", x), MyStr(x) => println!("String: {}", x), MyInt(x) => println!("Int: {}", x), } } fn main(){ print_value(MyBool(true)); // Following lines not working, how to write get_value func? // let a: bool = get_value(MyBool(true)); // println!("{}", a); } In case of struct it is simple, struct MyInt { value: int } struct MyBool{ value: bool } trait Value<S>{ fn get(&self) -> S; } impl Value<int> for MyInt{ fn get(&self) -> int{ self.value } } impl Value<bool> for MyBool{ fn get(&self) -> bool{ self.value } } fn get_value<S, T: Value<S>>(arg: T) -> S{ arg.get() } fn main(){ let a: bool = get_value(MyBool{value: true}); println!("{}", a); let b: int = get_value(MyInt{value: 100}); println!("{}", b); } Please help in writing generic function for enum. -- Regards Aravinda | ಅರವಿಂದ http://aravindavk.in
_______________________________________________ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev