I'd like to write a function which toggles/switches the provided value to the next in the enum and wraps around at the end:
enum Direction { NORTH, SOUTH, EAST, WEST }
For example, NORTH
=> SOUTH
, SOUTH
=> EAST
, EAST
=> WEST
, WEST
=> NORTH
.
Is there an easier way than manually creating a static array as described in In Rust, is there a way to iterate through the values of an enum?
use Direction::*;
static DIRECTIONS: [Direction; 4] = [NORTH, SOUTH, EAST, WEST];
Aren't enums suppose to be "enumerated"? I vaguely remember seeing an example before in Rust, but I can't seem to find it. Since Rust enums are more like unions/variants, I guess this complicates things.
I would prefer to explicitly encode the next direction via a match
statement:
#[derive(Debug)]
enum Direction {
North,
South,
East,
West,
}
impl Direction {
fn turn(&self) -> Self {
use Direction::*;
match *self {
North => South,
South => East,
East => West,
West => North,
}
}
}
fn main() {
use Direction::*;
for i in &[North, South, East, West] {
println!("{:?} -> {:?}", i, i.turn());
}
}
Vladimir's answer is correct but the programmer must remember to change the magic number "4" when adding a new member to the enum
. This definition for turn
should be easier to maintain:
#[derive(Debug, Copy, Clone, FromPrimitive)]
enum Direction {
NORTH = 0,
SOUTH,
EAST,
WEST,
}
fn turn(d: Direction) -> Direction {
match FromPrimitive::from_u8(d as u8 + 1) {
Some(d2) => d2,
None => FromPrimitive::from_u8(0).unwrap(),
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With