Function fmt_cmp::cmp::cmp[][src]

pub fn cmp<T: Display + ?Sized, U: Display + ?Sized>(
    lhs: &T,
    rhs: &U
) -> Ordering
Expand description

Compares two values in their Display representations.

This yields the same result as lhs.to_string().cmp(&rhs.to_string()) without heap allocation.

Note

This may call Display::fmt multiple times and if it emits different strings between the calls, the resulting Ordering value is unspecified.

Also, the Display implementations may not return error as described by the documentation of std::fmt. Doing so would result in an unspecified Ordering value or might even cause a panic in a future version.

Examples

Comparing digits of integers lexicographically:

assert!(fmt_cmp::cmp(&42, &240).is_gt());

Comparing format_args!:

assert!(fmt_cmp::cmp(&format_args!("{:X}", 0x2A), &format_args!("{:X}", 0x9)).is_le());