ToStr
2014-01-08
In order to have a default implementaton of to_str()
you need to add #[deriving(ToStr, Rand)]
#[deriving(ToStr, Rand)]
enum Weapons {
Sword,
Club,
Gaze
}
println(Club.to_str);
// => Club
The alternative, workin' in most cases, would be using format!
. The formatting string {:?}
means it will use reflection to display the struct name and values. This is similar to Ruby's #inspect
.
enum Monsters {
Goblin,
Orc
}
fn main() {
println(format!("{:?}", Orc));
}
// => Orc
blog comments powered by Disqus