Rust
E0600 Cannot Apply Unary Operator Fix
A unary operator is not built into the syntax for every type - it is a trait call in disguise. `-x` asks for the Neg trait and `!x` asks for the Not trait, and when the type of x implements neither the operator nor the one that was typed, the compiler refuses the expression with E0600 and names both the operator and the type. This guide separates the eleven shapes that produce it and matches each one to its repair: convert the value to a type that supports the operator, use the operator the type actually supports, or implement the missing trait for your own type.
Written November 25, 2026 · Tested with Rust 1.99.1 · 29 min read
- The rule underneath: each operator is one trait
- How to read the diagnostic
- The first failure: negating a u32
- The second failure: the error index example
Written November 25, 2026
Tested with Rust 1.99.1
What this guide is
Error E0600 is a unary operator applied to a type that does not support it. The error codes index describes it as an unary operator used on a type which does not implement it, and its example applies ! to a variant of an enum that has no Not implementation. There are only two unary operators that produce this error in the shapes covered here: -, which is the Neg trait, and !, which is the Not trait. The operator itself is never the problem - the pairing of operator and type is.
The forms in this guide each reproduce E0600 with a small, complete program, and the pairings differ each time: an unsigned integer negated, an enum inverted, a bool negated, a String negated and inverted, a char negated, a float inverted, two plain structs given one operator each, a vector negated as a whole, and an Option negated as a whole. The repairs fall into a few groups. Convert the value to a type that supports the operator - an unsigned integer to a signed one, a digit character or a numeric string to a number. Use the operator the type does support - ! for a bool, - for a float. Apply the operator to the parts instead of the whole - element by element for a vector, inside the Some for an option. Or, for a type the program owns, implement Neg or Not and the operator works as written.
Two neighboring results are not E0600 and are worked only in the traps below. A cast a type does not support is E0605, and dereferencing a value that is not a pointer is E0614 - both are unary-looking expressions with their own codes. Binary operator failures are different again: adding two bool values is E0369, and adding an integer to a String is E0308.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: each operator is one trait
The operator expressions section of the Reference sums up the two operators in one table. For -, the integer column says negation, with the footnote that this is only for signed integer types, the floating point column says negation, the bool column is empty, and the overloading trait is std::ops::Neg. For !, the integer column says bitwise NOT, the bool column says logical NOT, the floating point column is empty, and the overloading trait is std::ops::Not. Every E0600 in this guide is a pairing that table leaves empty: - on an unsigned integer, a bool, or a text type; ! on a float or a text type; either operator on a type of your own that implements neither trait.
The trait pages show the same split from the other side. The implementor list for Neg covers the signed integer types and the floating point types - and, notably, references to them, which is why one of the compiling neighbors below works through a &. The implementor list for Not covers bool and every integer type, signed and unsigned, and references to those as well. No text type, no collection, and no Option appears on either list, and a struct or enum of your own appears only when you write the implementation yourself.
That is also why there is no single fix for E0600. The diagnostic names a missing pairing, and the repair is whichever move supplies it: change the type before the operator, change the operator to the one the type has, take the value apart and apply the operator to the parts, or write the trait implementation for a type you own.
How to read the diagnostic
Every failure in the eleven families prints the same header shape - the code, the operator in backticks, and the type it was applied to - and then the notes vary with the pairing. The first family's, in full:
$ rustc --edition 2021 fail_neg_u32.rs -o fail_neg_u32
error[E0600]: cannot apply unary operator `-` to type `u32`
--> fail_neg_u32.rs:3:13
|
3 | let y = -x;
| ^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
Read it in order. The header states the whole problem in one line: the operator -, the type u32, and the refusal. The span underlines just the operator and repeats "cannot apply unary operator -". The note under the span is the part that changes from family to family. Here it is a reason - "unsigned values cannot be negated" - because an unsigned type has no negative values for the result to hold. For a struct or enum of your own, the note instead names the missing trait and points at the type definition, as the second and eighth families show. For ! on an f64, the sixth family, there is no note at all: the header is the entire explanation, because no standard trait pairing is being missed by a small margin - floats simply have no Not.
That variation is the fastest guide to the repair before touching code. A note about unsigned values points at a conversion to a signed type. A note naming Neg or Not as missing for your own type points at an implementation, or at taking the value apart. No note at all usually means the operator itself was the wrong choice for the value in hand.
The first failure: negating a u32
The classic shape - a minus sign in front of an unsigned integer:
fn main() {
let x: u32 = 5;
let y = -x;
println!("{}", y);
}
$ rustc --edition 2021 fail_neg_u32.rs -o fail_neg_u32
error[E0600]: cannot apply unary operator `-` to type `u32`
--> fail_neg_u32.rs:3:13
|
3 | let y = -x;
| ^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
An unsigned type holds no negative values, so there is nowhere for the result of -x to live, and the note under the span says exactly that. The fix converts the value to a signed type first, and negates that:
fn main() {
let x: u32 = 5;
let y = -(x as i32);
println!("{}", y);
}
$ rustc --edition 2021 fix_neg_u32.rs -o fix_neg_u32
$ ./fix_neg_u32
-5
The output is -5. The cast changes the type before the operator sees the value, and i32 implements Neg, so the same minus sign now works. The conversion is only honest while the value fits the signed type - a u32 above the i32 maximum does not survive the cast - so for values that can span the full unsigned range, the wider signed type (i64 for a u32) is the safer target, and the wrapping alternative below is the route when the value must stay unsigned.
The second failure: the error index example
The example from the error codes index - ! applied to an enum variant:
enum Question {
Yes,
No,
}
fn main() {
let y = !Question::Yes;
println!("{}", y);
}
$ rustc --edition 2021 fail_not_question.rs -o fail_not_question
error[E0600]: cannot apply unary operator `!` to type `Question`
--> fail_not_question.rs:6:13
|
6 | let y = !Question::Yes;
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
|
note: an implementation of `Not` might be missing for `Question`
--> fail_not_question.rs:1:1
|
1 | enum Question {
| ^^^^^^^^^^^^^ must implement `Not`
note: the trait `Not` must be implemented
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/bit.rs:35:1
|
35 | pub const trait Not {
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
The diagnostic adds two notes here: an implementation of Not might be missing for Question, and the trait Not must be implemented. The index page's own repair is that implementation - Not with output type bool, mapping each variant to its answer:
use std::ops::Not;
enum Question {
Yes,
No,
}
impl Not for Question {
type Output = bool;
fn not(self) -> bool {
match self {
Question::Yes => false,
Question::No => true,
}
}
}
fn main() {
println!("{} {}", !Question::Yes, !Question::No);
}
$ rustc --edition 2021 fix_not_question.rs -o fix_not_question
$ ./fix_not_question
false true
The output is false true: !Question::Yes is false, and !Question::No is true. An enum is a type the program owns, so supplying the trait is the direct fix - the operator stays, and the type gains the meaning the operator needs. The output type does not have to be bool; it is whatever ! on this type should produce, and this implementation follows the index page in choosing bool.
The third failure: negating a bool
A minus sign in front of a boolean:
fn main() {
let b = true;
let y = -b;
println!("{}", y);
}
$ rustc --edition 2021 fail_neg_bool.rs -o fail_neg_bool
error[E0600]: cannot apply unary operator `-` to type `bool`
--> fail_neg_bool.rs:3:13
|
3 | let y = -b;
| ^^ cannot apply unary operator `-`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
A bool is not a number, and the Reference table leaves its - column empty - there is no negation for a truth value. Almost every program that writes -b means the other operator: the fix keeps the value and changes the sign to !, which for a bool is logical NOT:
fn main() {
let b = true;
let y = !b;
println!("{}", y);
}
$ rustc --edition 2021 fix_neg_bool.rs -o fix_neg_bool
$ ./fix_neg_bool
false
The output is false. The pairing, not the value, was wrong: bool implements Not and not Neg, so swapping the operator is the whole repair. The same confusion in reverse - ! on a signed number, expecting a sign flip - is not an error at all, because integers implement Not as bitwise NOT; that shape is in the compiling neighbors, with its surprising output.
The fourth failure: negating a String
A minus sign in front of a String:
fn main() {
let s = String::from("5");
let y = -s;
println!("{}", y);
}
$ rustc --edition 2021 fail_neg_string.rs -o fail_neg_string
error[E0600]: cannot apply unary operator `-` to type `String`
--> fail_neg_string.rs:3:13
|
3 | let y = -s;
| ^^ cannot apply unary operator `-`
|
note: `String` does not implement `Neg`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:353:1
|
353 | pub struct String {
| ^^^^^^^^^^^^^^^^^ `String` is defined in another crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
A String is text, and no arithmetic operator applies to it as text. Where the string holds a number - the common case behind this error - the repair is to parse the text into a number first and negate the number:
fn main() {
let s = String::from("5");
let n: i32 = s.parse().unwrap();
let y = -n;
println!("{}", y);
}
$ rustc --edition 2021 fix_neg_string.rs -o fix_neg_string
$ ./fix_neg_string
-5
The output is -5: the string 5 parses to the integer 5, and the minus applies to that. Where the string does not hold a number, there is no repair of this form, because there is nothing to negate - the operator has to go, and the program works with the text as text.
The fifth failure: negating a char
A minus sign in front of a character:
fn main() {
let c = '5';
let y = -c;
println!("{}", y);
}
$ rustc --edition 2021 fail_neg_char.rs -o fail_neg_char
error[E0600]: cannot apply unary operator `-` to type `char`
--> fail_neg_char.rs:3:13
|
3 | let y = -c;
| ^^ cannot apply unary operator `-`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
A char is a single character, not a number, and it implements neither Neg nor Not - the ! form of this shape, !c on the same character, fails with the same code and the same header shape, naming ! and char. Where the character is a digit and the intent is its numeric value, the fix converts the digit to a number and negates that:
fn main() {
let c = '5';
let y = -(c.to_digit(10).unwrap() as i32);
println!("{}", y);
}
$ rustc --edition 2021 fix_neg_char.rs -o fix_neg_char
$ ./fix_neg_char
-5
The output is -5: to_digit reads the character 5 as the value 5, and the minus applies to the resulting integer. For a character that is not a digit, to_digit returns None and there is no numeric value to negate - the same position as the String family, one character at a time.
The sixth failure: applying ! to an f64
An exclamation mark in front of a float:
fn main() {
let x = 1.5f64;
let y = !x;
println!("{}", y);
}
$ rustc --edition 2021 fail_not_f64.rs -o fail_not_f64
error[E0600]: cannot apply unary operator `!` to type `f64`
--> fail_not_f64.rs:3:13
|
3 | let y = !x;
| ^^ cannot apply unary operator `!`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
This diagnostic is the shortest in the guide - the header and the span, with no note and no help - because the pairing is not close to anything standard: the Reference table leaves the floating point column for ! empty, and Not has no float implementors. The usual intent behind !x on a float is a sign flip typed with the wrong operator, and that repair is the minus sign, which floats do implement:
fn main() {
let x = 1.5f64;
let y = -x;
println!("{}", y);
}
$ rustc --edition 2021 fix_not_f64.rs -o fix_not_f64
$ ./fix_not_f64
-1.5
The output is -1.5. Where the intent was instead a yes-or-no question about the float - is it zero, is it not a number - no operator answers it, and the repair is a comparison or a method call; that form is in the alternatives below.
The seventh failure: applying ! to a String
An exclamation mark in front of a String:
fn main() {
let s = String::from("hi");
let y = !s;
println!("{}", y);
}
$ rustc --edition 2021 fail_not_string.rs -o fail_not_string
error[E0600]: cannot apply unary operator `!` to type `String`
--> fail_not_string.rs:3:13
|
3 | let y = !s;
| ^^ cannot apply unary operator `!`
|
note: `String` does not implement `Not`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:353:1
|
353 | pub struct String {
| ^^^^^^^^^^^^^^^^^ `String` is defined in another crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
! on a String fails for the same reason - did in the fourth family: text implements neither operator trait. The common intent here is a test on the text - most often whether it is empty - and the fix applies ! to the result of that test, which is a bool and does implement Not:
fn main() {
let s = String::from("hi");
let y = !s.is_empty();
println!("{}", y);
}
$ rustc --edition 2021 fix_not_string.rs -o fix_not_string
$ ./fix_not_string
true
The output is true: the string hi is not empty, so s.is_empty() is false and its negation is true. The operator moved, not the value - ! now applies to the boolean the method returns, which is a pairing the compiler accepts.
The eighth failure: negating a plain struct
A minus sign in front of a struct value, where the struct is a single signed field in a plain wrapper:
struct Point {
x: i32,
}
fn main() {
let p = Point { x: 3 };
let y = -p;
println!("{}", y.x);
}
$ rustc --edition 2021 fail_neg_struct.rs -o fail_neg_struct
error[E0600]: cannot apply unary operator `-` to type `Point`
--> fail_neg_struct.rs:6:13
|
6 | let y = -p;
| ^^ cannot apply unary operator `-`
|
note: an implementation of `Neg` might be missing for `Point`
--> fail_neg_struct.rs:1:1
|
1 | struct Point {
| ^^^^^^^^^^^^ must implement `Neg`
note: the trait `Neg` must be implemented
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:690:1
|
690 | pub const trait Neg {
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
The notes name the missing trait - an implementation of Neg might be missing for Point, and the trait must be implemented - and for a type the program owns, that is the repair. Neg takes the value by value and returns its Output; here the output is the same struct with its field negated:
use std::ops::Neg;
struct Point {
x: i32,
}
impl Neg for Point {
type Output = Self;
fn neg(self) -> Self {
Point { x: -self.x }
}
}
fn main() {
let p = Point { x: 3 };
let y = -p;
println!("{}", y.x);
}
$ rustc --edition 2021 fix_neg_struct.rs -o fix_neg_struct
$ ./fix_neg_struct
-3
The output is -3. The implementation is short because the struct is a wrapper around one signed value; a struct with several fields needs a decision per field about what negation means, and a struct wrapping an unsigned or text field may have no honest Neg at all - in which case the operator should stay unwritten and the field should be reached directly.
The ninth failure: applying ! to a plain struct
An exclamation mark in front of a struct holding a boolean:
struct Flag {
on: bool,
}
fn main() {
let f = Flag { on: true };
let y = !f;
println!("{}", y.on);
}
$ rustc --edition 2021 fail_not_struct.rs -o fail_not_struct
error[E0600]: cannot apply unary operator `!` to type `Flag`
--> fail_not_struct.rs:6:13
|
6 | let y = !f;
| ^^ cannot apply unary operator `!`
|
note: an implementation of `Not` might be missing for `Flag`
--> fail_not_struct.rs:1:1
|
1 | struct Flag {
| ^^^^^^^^^^^ must implement `Not`
note: the trait `Not` must be implemented
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/bit.rs:35:1
|
35 | pub const trait Not {
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
The same shape as the eighth family with the other trait: Flag implements no Not, so !f is refused. The fix implements Not, returning the same struct with its field inverted:
use std::ops::Not;
struct Flag {
on: bool,
}
impl Not for Flag {
type Output = Self;
fn not(self) -> Self {
Flag { on: !self.on }
}
}
fn main() {
let f = Flag { on: true };
let y = !f;
println!("{}", y.on);
}
$ rustc --edition 2021 fix_not_struct.rs -o fix_not_struct
$ ./fix_not_struct
false
The output is false: the flag that was on is off. As with the enum family, the output type is a choice - this implementation returns Self because inverting a flag naturally produces a flag; the enum in the second family returns bool instead, because there the answer, not another question, was the point.
The tenth failure: negating a Vec
A minus sign in front of a whole vector of integers:
fn main() {
let v: Vec<i32> = vec![1, 2];
let y = -v;
println!("{:?}", y);
}
$ rustc --edition 2021 fail_neg_vec.rs -o fail_neg_vec
error[E0600]: cannot apply unary operator `-` to type `Vec<i32>`
--> fail_neg_vec.rs:3:13
|
3 | let y = -v;
| ^^ cannot apply unary operator `-`
|
note: `Vec<i32>` does not implement `Neg`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:436:1
|
436 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Vec<i32>` is defined in another crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
The header names the vector type, Vec<i32> - the elements being signed does not help, because Neg would have to hold for the vector itself, and no collection type implements it. The intent is almost always to negate the elements, and that repair applies the operator inside the collection instead of to it:
fn main() {
let v: Vec<i32> = vec![1, 2];
let y: Vec<i32> = v.iter().map(|n| -n).collect();
println!("{:?}", y);
}
$ rustc --edition 2021 fix_neg_vec.rs -o fix_neg_vec
$ ./fix_neg_vec
[-1, -2]
The output is [-1, -2]: each element negated, the vector rebuilt from the mapped values. The operator moved from the whole to the parts - the same move as the Option family that follows, and the general answer whenever a container type is given an operator that only its contents support.
The eleventh failure: negating an Option
A minus sign in front of an Option holding an unsigned integer:
fn main() {
let o = Some(5u32);
let y = -o;
println!("{:?}", y);
}
$ rustc --edition 2021 fail_neg_option.rs -o fail_neg_option
error[E0600]: cannot apply unary operator `-` to type `Option<u32>`
--> fail_neg_option.rs:3:13
|
3 | let y = -o;
| ^^ cannot apply unary operator `-`
|
note: `Option<u32>` does not implement `Neg`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:598:1
|
598 | pub enum Option<T> {
| ^^^^^^^^^^^^^^^^^^ `Option<u32>` is defined in another crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0600`.
Two pairings are missing at once here: Option implements no Neg, and the value inside is unsigned, which has no Neg either. The fix unwraps one layer with map, converts the inner value to a signed type, and negates that - both repairs from the earlier families composed:
fn main() {
let o = Some(5u32);
let y = o.map(|n| -(n as i32));
println!("{:?}", y);
}
$ rustc --edition 2021 fix_neg_option.rs -o fix_neg_option
$ ./fix_neg_option
Some(-5)
The output is Some(-5): the option is still a Some, and the value inside it is negated. Had the option been None, map would have skipped the closure entirely and the result would have stayed None - the container decides whether the operator runs at all, which is exactly the behavior negating an optional value should have.
Seven more shapes that fail the same way
The same header - the operator in backticks and the type it was applied to - for seven more programs. Each fails with exactly one E0600 and no other error, and each is repaired by the same moves as the families above: convert first, change the operator, apply it to the parts, or implement the trait:
Every other unsigned integer type negates the same way - here a u8:
fn main() {
let x: u8 = 5;
let y = -x;
println!("{}", y);
}
And the pointer-sized unsigned type, usize:
fn main() {
let x: usize = 5;
let y = -x;
println!("{}", y);
}
The smaller float takes ! no better than the f64 of the sixth family:
fn main() {
let x = 1.5f32;
let y = !x;
println!("{}", y);
}
The unit type, (), has no negation:
fn main() {
let u = ();
let y = -u;
println!("{:?}", y);
}
An array of bool values is not itself a bool, and ! does not reach inside it:
fn main() {
let a = [true, false];
let y = !a;
println!("{:?}", y);
}
A string slice, &str, fails exactly as the owned String of the fourth family did - being a reference to text does not add an operator:
fn main() {
let s = "hi";
let y = -s;
println!("{}", y);
}
The position of the expression changes nothing - here the negation is a function's return expression, on a u64:
fn get() -> u64 {
let x: u64 = 5;
-x
}
fn main() {
println!("{}", get());
}
When the value has to stay unsigned
Two repairs cover intents the families above route around.
Stay unsigned and wrap, with wrapping_neg. Where the program genuinely needs the two's complement wrap of an unsigned value - the value that negation would produce if the type could hold it - the method computes it without any operator:
fn main() {
let x: u32 = 5;
let y = x.wrapping_neg();
println!("{}", y);
}
$ rustc --edition 2021 alt_wrapping_neg.rs -o alt_wrapping_neg
$ ./alt_wrapping_neg
4294967291
The output is 4294967291, which is 2 to the power 32 minus 5 - the wrapped result, still a u32. This is not a way to get -5; it is the answer only when the wrapped value is what the program actually means, as in checksum and hashing arithmetic.
Turn a float into the boolean the ! was reaching for. Where !x on a float meant a yes-or-no question, the repair is the comparison written out:
fn main() {
let x = 1.5f64;
let y = x == 0.0;
println!("{}", y);
}
$ rustc --edition 2021 alt_float_bool_test.rs -o alt_float_bool_test
$ ./alt_float_bool_test
false
The output is false: 1.5 is not zero. The ! then belongs in front of the comparison if the opposite answer is wanted - !(x == 0.0) applies Not to a bool, a pairing that works.
The neighbors that compile
These programs apply the same two operators with no error, each because the pairing is one the traits cover.
Negating a signed integer: i32 implements Neg, and the result is the ordinary negative:
fn main() {
let x = 5i32;
let y = -x;
println!("{}", y);
}
$ rustc --edition 2021 ok_neg_i32.rs -o ok_neg_i32
$ ./ok_neg_i32
-5
The output is -5. This is the shape the first family is the failure of: same operator, same value, and the only difference is the signedness of the type.
Applying ! to a bool: logical NOT, the pairing the third family needed:
fn main() {
let b = true;
let y = !b;
println!("{}", y);
}
$ rustc --edition 2021 ok_not_bool.rs -o ok_not_bool
$ ./ok_not_bool
false
The output is false.
Applying ! to an unsigned integer: integers implement Not as bitwise NOT, so this compiles - and the result surprises anyone who expected a sign flip:
fn main() {
let x: u32 = 5;
let y = !x;
println!("{}", y);
}
$ rustc --edition 2021 ok_not_u32.rs -o ok_not_u32
$ ./ok_not_u32
4294967290
The output is 4294967290: every bit of 5 flipped, which as an unsigned value is 2 to the power 32 minus 6. ! on an integer is a bit operation, never a negation, and on a signed integer it gives the two's complement neighbor - !5 on an i32 is -6.
Negating a float: floats implement Neg even though they have no Not, which is the asymmetry the sixth family ran into from the other side:
fn main() {
let x = 1.5f64;
let y = -x;
println!("{}", y);
}
$ rustc --edition 2021 ok_neg_f64.rs -o ok_neg_f64
$ ./ok_neg_f64
-1.5
The output is -1.5.
Negating through a reference: the Neg implementor list includes references to the signed types, so - applied to a &i32 compiles and negates the value behind the reference:
fn main() {
let x = 5;
let r = &x;
let y = -r;
println!("{}", y);
}
$ rustc --edition 2021 ok_neg_ref.rs -o ok_neg_ref
$ ./ok_neg_ref
-5
The output is -5. The same holds for ! through a reference to a bool, because the Not list includes references to its types as well:
fn main() {
let b = true;
let r = &b;
let y = !r;
println!("{}", y);
}
$ rustc --edition 2021 ok_not_ref_bool.rs -o ok_not_ref_bool
$ ./ok_not_ref_bool
false
The output is false. A single shared reference forwards an operator only where the standard library implements the trait for that reference - - on a &i32 and ! on a &bool, as above. It does not forward an operator to a type that lacks that pairing, as the &str shape in the seven more shapes above shows - text behind a reference is still text - and neither a mutable reference nor a double reference forwards either operator: - on a &mut i32 or a &&i32, and ! on a &mut bool or a &&bool, are E0600 as well.
The fixes in reach order
- The operator was typed for the wrong pairing: swap it -
!for abool,-for a signed number or a float. The third and sixth families are this repair, and it is the first thing to check. - The value is unsigned and a negative result is wanted: convert to a signed type first -
-(x as i32), or a wider signed type when the value can be large. The first family. - The value is text holding a number: parse it, or read the digit, and negate the number. The fourth and fifth families. Text that holds no number has nothing to negate.
- The value is a test result in disguise: call the method or write the comparison, and apply
!to the resultingbool. The seventh family, and the float alternative. - The operator was applied to a container: apply it to the contents instead -
mapover the elements or the inner value. The tenth and eleventh families. - The type is your own: implement
NegorNot. The second, eighth, and ninth families. The implementation must be honest - a field that cannot be negated or inverted means the type should not carry the operator. - The value must stay unsigned and wrap:
wrapping_neg, and only when the wrapped result is the intent. - The header is not E0600: a cast is E0605, a dereference of a non-pointer is E0614, and binary operator failures are E0369 or E0308 - all in the traps.
Traps
- A cast the types do not support is E0605, not E0600. Casting looks like a unary operation on the value, and it is often the next thing tried after a failed negation of a
String- but an unsupported cast has its own code:
fn main() {
let s = String::from("hi");
let y = s as i32;
println!("{}", y);
}
$ rustc --edition 2021 trap_cast.rs -o trap_cast
error[E0605]: non-primitive cast: `String` as `i32`
--> trap_cast.rs:3:13
|
3 | let y = s as i32;
| ^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0605`.
* operator is the third unary prefix operator, and it does not route through Neg or Not at all:fn main() {
let x = 5;
let y = *x;
println!("{}", y);
}
$ rustc --edition 2021 trap_deref.rs -o trap_deref
error[E0614]: type `{integer}` cannot be dereferenced
--> trap_deref.rs:3:13
|
3 | let y = *x;
| ^^ can't be dereferenced
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0614`.
The header says the type cannot be dereferenced. An integer is already a value, not a pointer to one, so there is nothing for * to do.
- A binary operator on an unsupported pairing is E0369, the two-operand counterpart of E0600. Adding two bool values:
fn main() {
let y = true + true;
println!("{}", y);
}
$ rustc --edition 2021 trap_binary_bool.rs -o trap_binary_bool
error[E0369]: cannot add `bool` to `bool`
--> trap_binary_bool.rs:2:18
|
2 | let y = true + true;
| ---- ^ ---- bool
| |
| bool
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
The shape of the refusal is the same idea with two types named instead of one - the pairing, this time of two operands and a binary operator, has no trait implementation behind it.
- Adding an integer to a String is not even E0369 - it is E0308, a type mismatch, because String does have an addition implementation and it expects a string slice:
fn main() {
let s = String::from("hi");
let y = s + 1;
println!("{}", y);
}
$ rustc --edition 2021 trap_string_plus.rs -o trap_string_plus
error[E0308]: mismatched types
--> trap_string_plus.rs:3:17
|
3 | let y = s + 1;
| ^ expected `&str`, found integer
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
The span says "expected &str, found integer": the operator exists for the type, and the other operand is what does not fit. Reading which of the three codes - E0600, E0369, E0308 - a failed operator expression produced is the quickest way to tell whether the operator, the pairing, or the operand is at fault.
Checklist
- The header checked: it names the operator and the type, and that pairing - not the value, not the position of the expression - is what failed.
- The note under the span read before editing: a reason (unsigned values cannot be negated), a missing trait named for your own type, or no note at all, and each points at a different repair.
- The intended meaning settled before the repair: a negative value, an inverted value, a bit flip, or a yes-or-no answer - the four intents these two operators are confused among, each with a different fix.
- For a conversion fix, the target type checked for range: a cast to a signed type truncates values above its maximum, so the wider signed type is chosen when the input can be large.
- For a container, the operator applied to the contents, and the empty case accounted for - an empty vector maps to an empty vector, and a
NonestaysNone. - For a trait implementation on your own type, every field given an honest negation or inversion, or the operator left unimplemented.
!on an integer never mistaken for negation in existing code that compiles - it is bitwise NOT, and its result on unsigned values is a large positive number.- The fixed program run, and its output checked against what the program was meant to print.
Related guides on this site
- E0368 Assignment Operation Cannot Apply Fix
- E0161 Cannot Move Unsized Value Fix
- Cannot Transmute Zero-Sized Type E0591 Fix
- E0501 Cannot Borrow as Immutable Fix
Sources
- Rust, Error codes index: E0600 — for the definition of the error as an unary operator used on a type which does not implement it, for its example of applying
!to an enum variant, and for its repair of implementing theNottrait for the enum, with output typebool. - The Rust Reference, Operator expressions — for the table of the two unary operators:
-as negation for signed integer and floating point types only,!as bitwise NOT for integers and logical NOT forbool, and theNegandNottraits as the overloading traits for each. - The Rust Standard Library, Neg — for
Negas the unary negation operator trait, for its requirednegmethod andOutputtype, and for its implementor list covering the signed integer and floating point types and references to them. - The Rust Standard Library, Not — for
Notas the unary logical negation operator trait, for its requirednotmethod andOutputtype, and for its implementor list coveringbool, every integer type, and references to them.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.