October 2018
Beginner
180 pages
4h 48m
English
When the program runs, it can take different actions depending on the specific enumeration type it's looking at, and it has access to the data values that were stored as parameters. We've already seen the best tool for doing that, match:
for step in directions.iter() { match step { Drive::Forward(blocks) => { println!("Drive forward {} blocks", blocks); }, Drive::Turn{slight, right} => { println!("Turn {}{}", if *slight { "slightly " } else { "" }, if *right { "right" } else { "left" } ); }, Drive::Stop => { println!("You have reached your destination"); } }; };
Here, we have a for loop that processes each item in our array of directions, one at a time, starting with the first one. ...
Read now
Unlock full access