Storing Strings in an Array
Hands-on Rust — by Herbert Wolverson (27 / 120)
👈 Trimming Input | TOC | Grouping Data with Structs 👉
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 input to lowercase, so you need to compare with lowercase strings for there to be a match.
If you have a second friend named “Steve,” you could also allow them inside by using an or expression. Or in Rust is expressed with ||:
if your_name == "bert" || your_name == "steve" {
Booleans
INFORMATION
Combining comparisons is called Boolean logic. || means “or.” && means “and.” ! means “not.” You’ll use these throughout the book. But don’t worry, they’ll be explained when…