Check If Two Variable Is Not Equal In Rust
Code snippet for how to Check If Two Variable Is Not Equal In Rust with sample and detail explanation
Rust language allows for several methods to check if two variables are not equal. This article focuses on demonstrating and explaining one such simple method.
Code snippet to Check If Two Variables Are Not Equal In Rust
let var1 = 5;
let var2 = 10;
if var1 != var2 {
println!("The variables are not equal");
} else {
println!("The variable are equal");
}
Code Explanation for Checking If Two Variables Are Not Equal In Rust
In the provided Rust code snippet, we initialize two variables, var1
and var2
, with values 5
and 10
respectively.
The core section of the code lies in the if
statement. In Rust language, inequality between two variables can be checked using the !=
operator. The if var1 != var2
statement essentially checks whether var1
is not equal to var2
.
Upon executing this code, if the condition found within the if
statement — var1 != var2
— stands true, then the code block following this statement is run. Thus ‘println!(“The variables are not equal”);’ is printed, displaying the text “The variables are not equal” on the console.
Alternatively, when the condition within the if
statement turns out to be false, then the code block connected to the else
statement is executed. Thus, the code ‘println!(“The variables are equal”);’ is run, leading to the print-out of the text “The variables are equal” on the console.
As per the values we provided (5
and 10
), the output on running this particular code snippet will be “The variables are not equal”.
This way, you can check whether two variables are not equal in Rust using the !=
operator within an if
statement. Go ahead and try it out with different variable values and explore the results!