Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The > (greater than) operator is a binary comparison operator that evaluates whether the left-hand operand is strictly greater than the right-hand operand, returning a bool. In Rust, this operator is syntactic sugar for the gt method provided by the std::cmp::PartialOrd trait.

Syntax

left_operand > right_operand

Trait Mechanics

When the compiler encounters the > operator, it translates the expression into a fully qualified function call to the PartialOrd trait. The expression a > b desugars to std::cmp::PartialOrd::gt(&a, &b). It is not strictly equivalent to the method call syntax a.gt(&b). The method call syntax is subject to dot-operator method resolution rules (such as auto-referencing and auto-dereferencing) and could potentially resolve to an inherent method named gt on the type rather than the PartialOrd trait method. The > operator bypasses this ambiguity by directly invoking the trait.
pub trait PartialOrd<Rhs = Self>: PartialEq<Rhs> {
    fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;

    // The `>` operator maps to this provided method
    fn gt(&self, other: &Rhs) -> bool {
        matches!(self.partial_cmp(other), Some(Ordering::Greater))
    }
}
To use the > operator, the type of the left operand must implement PartialOrd<Rhs> where Rhs is the type of the right operand. Because PartialOrd requires PartialEq as a supertrait, types compared with > must also support equality comparisons.

Evaluation Rules

The behavior of the > operator depends on the underlying type implementation of partial_cmp:
  • Integers: Performs standard mathematical comparison.
  • Floating-Point Numbers (f32, f64): Adheres to IEEE 754 standards. Because floating-point numbers only implement PartialOrd and not Ord, comparisons involving NaN (Not-a-Number) will always evaluate to false, as NaN is unordered relative to any value, including itself.
  • Characters and Strings: Performs lexicographical comparison based on underlying Unicode scalar values.
  • Compound Types (Tuples, Arrays, Slices): Evaluates lexicographically from left to right. The operator compares elements at corresponding indices. If it finds a strictly greater element, it returns true. If the elements are equal, it proceeds to the next index. For sequences of unequal length (such as slices or strings) where the shorter sequence is a prefix of the longer sequence, the longer sequence is considered greater based on its length.
  • Structs and Enums: If #[derive(PartialOrd)] is applied, structs are compared lexicographically top-to-bottom based on field declaration order. Enums are compared based on discriminant order (top-to-bottom), followed by their payload.

Mechanical Demonstration

// Primitive numeric evaluation
let int_eval: bool = 10_i32 > 5_i32; // true

// Floating-point evaluation with NaN
let float_eval: bool = f64::NAN > 5.0; // false

// Lexicographical tuple evaluation
// Compares index 0 (2 == 2), then index 1 (3 > 1)
let tuple_eval: bool = (2, 3) > (2, 1); // true

// Lexicographical slice evaluation with length fallback
// The shorter slice is a prefix, so the longer slice is greater
let slice_eval: bool = [1, 2, 3].as_slice() > [1, 2].as_slice(); // true

// Explicit trait method invocation (compiler translation)
let explicit_eval: bool = std::cmp::PartialOrd::gt(&10_i32, &5_i32); // true
Master Rust with Deep Grasping Methodology!Learn More