Storing Strings in an Array
The treehouse is an exclusive club. Only individuals on the approved list of friends are allowed inside. You could accomplish this with if statements. If you only want to admit your friend “Bert,” you could use the following code:
| if your_name == "bert" { |
| println!("Welcome."); |
| } else { |
| println!("Sorry, you are not on the list.") |
| } |
The if/else control flow system works much like it does in other programming languages: if the condition is true, then it executes the code in the first block. Otherwise, it runs the code in the else block.
When you do your comparison, make sure that you compare your string with “bert” in lowercase. You need to use lowercase because the input function converts the user’s ...
Get Hands-on Rust now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.