You are currently viewing Rust Relational Operators

Rust Relational Operators

Relational operators in Rust are essential in comparing values and making decisions based on their relationships. These operators allow you to determine whether one value is greater than, less than, equal to, or not equal to another. In this article, we’ll explore Rust’s relational operators, their functionalities, and provide code examples to solidify your understanding.

Understanding Relational Operators

Rust provides a set of relational operators that help developers compare values. The basic relational operators are:

  • Equal To (==)
  • Not Equal To (!=)
  • Less Than (<)
  • Greater Than (>)
  • Less Than or Equal To (<=)
  • Greater Than or Equal To (>=)

These operators return a boolean value (true or `false) based on the evaluation of the specified relationship between two operands.

Equality Operator (==)

The equality operator, represented by ==, checks whether two values are equal. It returns a boolean true if the values are identical and false otherwise. Here’s an example:

fn main() {

    let a = 5;
    let b = 5;

    if a == b {
        println!("a is equal to b");
		
    } else {
        println!("a is not equal to b");
		
    }
	
}

In this example, the program compares the values of a and b. Since both are equal, it will print “a is equal to b.”

Inequality Operator (!=)

On the flip side, the inequality operator, denoted by !=, evaluates whether two values are not equal. It returns true if the values are different and false if they are the same. Here’s an example:

fn main() {

    let a = 10;
    let b = 20;

    if a != b {
        println!("a is not equal to b");
		
    } else {
        println!("a is equal to b");
		
    }
	
}

Here, the program compares a and b. Since they are not equal, it will print “a is not equal to b.”

Less Than Operator (<)

Moving on, the less than operator (<) checks if the left operand is strictly less than the right operand. If the condition is met, it returns true; otherwise, it returns false. Here’s an example:

fn main() {

    let a = 15;
    let b = 25;

    if a < b {
        println!("a is less than b");
		
    } else {
        println!("a is not less than b");
		
    }
	
}

In this example, since a is indeed less than b, the program will print “a is less than b.”

Greater Than Operator (>)

Conversely, the greater than operator (>) assesses if the left operand is greater than the right one. If the condition holds true, it returns true; otherwise, it returns false. Here’s an example:

fn main() {

    let a = 30;
    let b = 20;

    if a > b {
        println!("a is greater than b");
		
    } else {
        println!("a is not greater than b");
		
    }
	
}

Here, since a is greater than b, the program will print “a is greater than b.”

Less Than or Equal To Operator (<=)

The less than or equal to operator (<=) combines the functionalities of both less than and equal to operators. It returns true if the left operand is less than or equal to the right operand; otherwise, it returns false. Here’s an example:

fn main() {

    let a = 10;
    let b = 10;

    if a <= b {
        println!("a is less than or equal to b");
		
    } else {
        println!("a is greater than b");
		
    }
	
}

Since a is equal to b, the program will print “a is less than or equal to b.”

Greater Than or Equal To Operator (>=)

Similarly, the greater than or equal to operator (>=) combines the functionalities of both greater than and equal to operators. It returns true if the left operand is greater than or equal to the right operand; otherwise, it returns false. Here’s an example:

fn main() {

    let a = 25;
    let b = 20;

    if a >= b {
        println!("a is greater than or equal to b");
		
    } else {
        println!("a is less than b");
		
    }
	
}

As a is greater than b, the program will print “a is greater than or equal to b.”

Pattern Matching with Match

Rust’s match keyword provides a powerful way to use relational operators in a more expressive and flexible manner. It allows you to create patterns and execute different code blocks based on the value of a variable.

fn main() {

    let number = 42;

    match number {
	
        0 => println!("It's zero"),
        1 | 2 => println!("It's one or two"),
        n if n > 10 => println!("It's greater than 10"),
        _ => println!("It's something else"),
		
    }
	
}

In this example, the match statement evaluates the value of number and executes the corresponding block based on the defined patterns.

fn main() {

    let temperature = 25;

    match temperature {
        0..=10 => println!("It's very cold!"),
        11..=20 => println!("It's cold."),
        21..=30 => println!("It's warm."),
        _ => println!("It's hot!"),
    }
    
}

Here, the program uses the match statement to categorize the temperature into different ranges. The ..= operator denotes an inclusive range, and the underscore (_) acts as a wildcard for any other value. This example showcases how relational operators, when used with pattern matching, can make code more expressive and readable.

String Comparison

String comparison is a common use case for relational operators. Rust supports comparing strings using the standard ordering operators.

fn main() {

    let str1 = "Edward";
    let str2 = "Edwin";

    // String comparison
    if str1 < str2 {
        println!("str1 comes before str2");
		
    } else {
        println!("str1 comes after or is equal to str2");
		
    }
    
}

In this example, the program compares two strings, str1 and str2, using the less than (<) operator. Rust handles string comparison based on lexicographical order.

Conclusion

Understanding and effectively using relational operators in Rust is essential for building robust and functional Rust programs. These operators enable you to compare values and make decisions based on those comparisons, adding a layer of control and flexibility to your code. Whether you’re comparing values for equality, checking their order, or creating complex conditions, Rust’s relational operators provide the tools you need to make your code expressive and concise.

Sources:

Leave a Reply