R
RUSTY YELLOW PAGES

<=Operator


Explanation

<= is the less-than-or-equal comparison, provided by std::cmp::PartialOrd alongside <, >, and >= — implementing PartialOrd (usually via #[derive(PartialOrd)], which requires PartialEq as well) gives you all four ordering operators together, not just one.

Usage examples

Checking whether one value is at most another

let a = 3;
let b = 5;
let ok = a <= b; // <- true if `a` is less than or equal to `b`

Restriction: comparisons can't be chained like in Python — a <= b <= c doesn't compile; write a <= b && b <= c instead.

Validating input

Writing an inclusive range check as MIN <= x && x <= MAX mirrors how the range reads on paper, keeping both endpoints explicitly included.

struct Reading {
    celsius: f64,
}

const MIN_SAFE: f64 = -20.0;
const MAX_SAFE: f64 = 60.0;

fn in_safe_range(reading: &Reading) -> bool {
    MIN_SAFE <= reading.celsius && reading.celsius <= MAX_SAFE // <- `<=` twice expresses an inclusive range
}

MIN <= x && x <= MAX keeps both bounds inclusive explicitly, rather than reaching for a Range (MIN..MAX, which is half-open) where an inclusive upper bound is actually what's intended. The idiomatic form is (MIN..=MAX).contains(&x) — clippy's manual_range_contains lint suggests exactly that rewrite — but the explicit && chain remains fine when you want the bounds visible inline; see < for the fuller ordering-operator treatment.

Explanation

Embedded support: Full

<= means the same thing under #![no_std] — the same core::cmp PartialOrd as <, >, and >=. The inclusive-upper-bound reading comes up often in firmware for a retry counter that hasn't yet exceeded its cap (the capped attempt itself should still be allowed), and for the upper edge of an inclusive safe-operating range paired with >= on the lower edge.

Usage examples

Allowing a bus transaction retry up to and including its cap

const MAX_RETRIES: u8 = 3;

fn may_retry(attempt: u8) -> bool {
    attempt <= MAX_RETRIES // <- `<=` allows the boundary attempt itself, not just attempts strictly below it
}

Confirming a reading sits within an inclusive safe range

const MIN_SAFE_CELSIUS: i16 = -20;
const MAX_SAFE_CELSIUS: i16 = 85;

fn in_safe_range(temperature_celsius: i16) -> bool {
    MIN_SAFE_CELSIUS <= temperature_celsius && temperature_celsius <= MAX_SAFE_CELSIUS
    // <- `<=` twice expresses an inclusive range on both edges
}