Ruta graveolens  ·  notes from a language experiment  ·  cultivated since 2025

E0801: Cannot negate

CodeCategoryStability
E0801Literal and operatorPermanent

Explanation

Unary - requires a signed integer or floating-point operand. Unsigned integer types have no negative range, and non-numeric types such as bool or () are not negatable at all, so applying - to either is a compile-time error. The same rule rejects a negative integer literal pattern whose match scrutinee has an unsigned type, because such an arm could never match.

Likely cause

The operand's annotated or inferred type is an unsigned integer (u8 through u64, usize) or a non-numeric type. Give the value a signed integer or floating-point type, or compute the quantity you meant with subtraction on the unsigned type instead of negating it.

Examples

Negate an unsigned value

fn main() -> i32 {
    let count: u32 = 5;
    let negated = -count;
    0
}

Negate a signed value

fn main() -> i32 {
    let count: i32 = 5;
    -count
}

References