The three examples below just use simple example code. A little further down, we will implement one of those three methods in our real project.
Suppose we have a variable that can hold any one of several possibilities. For example, we might have a "flavor" variable that can be "apple, "peach", "banana", "strawberry", "grape", or "kiwi". It's easy to set that variable, but then we need to take action based on that variable's value. Here are three ways we can make decisions based on that variable.
Use an If-Then
This is a simple, easy-to-understand method.
Use a "match" Statement
This is a cleaner method. It also has a required "none of the above" option (the "_" option). We could (should?) have done a "none of the above" option with the above If-Then method, but it's not quite as easy there, so we didn't bother.
Use a hashmap
This method builds a one-to-one matching "table" (like a spreadsheet, sort of) that has one column that holds the flavor, and a second column that holds the matching value to that flavor. This method is a bit harder to wrap your brain around, but it's pretty slick.
You build a hashmap named "messages" which holds the "hash table" (or "look-up table", or "spreadsheet"); later, you send the chosen flavor to that hashmap, and it returns a "Return"-type package. Imagine United Parcel Service (UPS) delivering a package to your front door. Imagine that they deliver every package with a note on top that says either "OK" or "Error". When you open an "OK" package, you find the item you were expecting, but if you open an "Error" package, you find a broken item with a note that says, "This item was broken during shipping", or "This item was already broken when UPS picked it up", or something similar. This is similar to a "Return"-type of data in Rust. The proper thing to do is to examine the return type (is it an "Ok" or an "Err"?), and then proceed accordingly. But in our example above, we don't care that much about doing "the right thing" with any errors, so we just "unwrap()" the package without worrying about the "Ok" or "Err", trusting that whatever we find in the package will be usable for our purposes.
If We Were to Implement the HashMap Into Our "sl" Project
For our "sl" project, we can use any of the above three methods to convert a variable specifying the image to draw into the displaying of that image, but the If-Then method is probably the least-preferable. Since the hashmap method is probably less better-documented in the wild than is the match method, I'll show here how to use it, if for no other reason than to add to the world's documentation of how to use it, but I think we'll wind up going with the match method. But if we were to use the hashmap method, here's how our mods would look:
But, as mentioned, we'll instead use the match method. This will give us a little more flexibility. For example, the user running the program will be able to specify "p" or "P" or "plane" or "pLANe", etc, rather than a strict "PLANE". We'll set this up in the next lesson, Getting User-Provided Options on the Command-Line.