Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if both conditions to the left and right of it are true. For example:
10 == 10 && 7 < 10 // Evaluates to true
We have two conditions separated by && operator:
10 == 10, which evaluates to true7 < 10, which evaluates to true
An example inside an if statement:
let x = 10;
if x != 7 && 12 < x { // Evaluates to false
println!("This is true!");
}
Inside the if, we have two conditions separated by && operator:
x != 7, equivalent to10 != 7, which evaluates to true12 < x, equivalent to12 < 10, which evaluates to false
Assignment
Follow the Coding Tutorial and play with the and operator.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check