Rust comparison operator
Created By: chatGPT
Rust provides a variety of comparison operators to compare values. These operators can be used with different types of data such as integers, floats, and even strings. Understanding these operators is essential for implementing conditions and controlling the flow of your programs.
let a = 5;
let b = 10;
// Greater than
if a > b {
println!("a is greater than b");
}
// Less than
if a < b {
println!("a is less than b");
}
// Equal to
if a == b {
println!("a is equal to b");
}
// Not equal to
if a != b {
println!("a is not equal to b");
}
// Greater than or equal to
if a >= b {
println!("a is greater than or equal to b");
}
// Less than or equal to
if a <= b {
println!("a is less than or equal to b");
}
Operators can be combined in logical expressions. It’s common to use comparison operators in conjunction with logical operators (
&&
, ||
, and !
) to create more complex conditional statements.let x = 7;
let y = 3;
if x > y && y > 0 {
println!("y is positive and less than x");
}
if x < 5 || y < 5 {
println!("at least one variable is less than 5");
}