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.