E0369 Binary Operation Fix
An operator, two operands, and no implementation connecting them: a comparison with no PartialEq, an addition with no Add, a shift a float does not support. This guide separates the shapes that produce E0369 and matches each one to the trait to derive or implement, or to the type change that gives the operation an implementation to call.
Written November 23, 2026 · Tested with Rust 1.99.1 · 18 min read
- The rule underneath: an operator is a trait call, and the trait must exist for these operand types
- How to read the diagnostic
- The first failure: equality on a struct without PartialEq
- The second failure: inequality on an enum without PartialEq
Written November 23, 2026
Tested with Rust 1.99.1
What this guide is
Error E0369 is an operator with no implementation behind it. The error codes index states it in one line: a binary operation was attempted on a type which doesn't support it. Every operator in Rust is backed by a trait - == and != by PartialEq, < by PartialOrd, + by Add, * by Mul, << by Shl, % by Rem - and the operation compiles only when the left-hand type implements that trait for the right-hand type. The header names the missing connection in one of its forms: "binary operation == cannot be applied to type Point", "cannot add Vec2 to Vec2", "no implementation for f32 << {integer}". The index gives the whole diagnosis in its fix line: check that this type implements this binary operation, and notes that most operators can be overloaded for your own type by implementing traits from std::ops.
The forms in this guide each reproduce E0369 with a small, complete program, and each fails for a different reason. Equality on a struct that does not implement PartialEq. Inequality on an enum that does not implement it either. Addition on a struct with no Add implementation. A left shift on an f32, a type that deliberately has no shift. A less-than comparison on a struct with no PartialOrd. Multiplication on a struct with no Mul. Concatenation of two &str values, where the left side is not the owned String the operation requires. And a remainder on bool values, which implement no remainder at all. In each family the failing program is small, the full compiler output is shown, and the fix supplies what was missing: a derive, a trait implementation, a type that supports the operation, or an owned string on the left.
Four neighboring results are not E0369 and are worked only in the traps and the decision summary. Adding a String to a String is E0308, mismatched types, because String does implement Add - for a &str on the right. Adding an f64 to an i32 is E0277, a trait bound failure, because i32 implements Add for itself but not for f64. Summing custom values with an iterator's sum is also E0277. And negating a custom type with a unary - is E0600, the unary-operator code, not the binary one.
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: an operator is a trait call, and the trait must exist for these operand types
The Book's operator appendix lists each operator beside the trait that overloads it: == is PartialEq, != is PartialEq, < is PartialOrd, + is Add, * is Mul, << is Shl, % is Rem. The std::ops module makes the same point from the library side: implementing these traits allows you to overload certain operators, and only operators backed by traits can be overloaded. So a == b is not a built-in comparison the compiler performs on any two values - it is a call to PartialEq::eq, and a type that has not provided that trait, by derive or by a manual implementation, has no equality to call. The PartialEq documentation is explicit about what the trait provides: implementing it for a type provides the == and != operators for that type, and when it is derived on a struct, two instances are equal if all fields are equal; on an enum, if they are the same variant and all fields are equal. PartialOrd likewise provides the <, <=, >, and >= operators through its lt, le, gt, and ge methods, requires PartialEq, and when derived on a struct produces a lexicographic ordering from the declaration order of its fields.
That explains the two fix shapes in this guide. For comparison traits, a derive is usually the whole fix, because field-by-field equality and declaration-order ordering are what most code means by == and < - and where they are not, a manual implementation states the real rule, comparing only the fields that define equality. For arithmetic traits there is no derive: Add, Mul, and the rest are written by hand, with an associated Output type and one method that performs the operation field by field or in whatever terms the type means. The third shape is not a trait to add but a type to change: some built-in types omit an operation on purpose. An f32 has no shift, because shifting a floating-point value has no agreed meaning, and a bool has no remainder. No implementation can be added to those foreign types from your code, so the fix moves the value to a type that supports the operation - an integer for a shift, integers for a remainder - or rethinks what the operation was meant to compute.
One boundary in the diagnostics is worth learning before the families. When the left-hand type has no implementation of the operator's trait at all for any right-hand type, the compiler reports E0369. When the type does implement the trait, but not for the right-hand type in front of it, the failure is reported differently - as E0277, a trait bound that is not satisfied, or as E0308, an ordinary type mismatch against the implementation that does exist. The traps at the end work that boundary in both directions, and it is the fastest way to tell whether a missing trait or a wrong operand is the problem.
How to read the diagnostic
The header gives the operator and the types: "binary operation == cannot be applied to type Point", "cannot add Vec2 to Vec2", "cannot calculate the remainder of bool divided by bool". Read the operator first, because it names the trait - the appendix table above converts any operator in a header to the trait to look for. Then read the types on either side of it: the trait must be implemented by the left type for the right type, so a header naming two different types is a different problem from one naming the same type twice, even before any note is read.
What follows the header separates the families. A note reading "an implementation of PartialEq might be missing for Point", pointing at the type's definition with "must implement PartialEq", confirms the no-implementation case, and a help reading "consider annotating Point with #[derive(PartialEq)]" performs the fix when a derive is available - for PartialOrd the help offers both traits together, #[derive(PartialEq, PartialOrd)], because ordering requires equality. A second note, "the trait Add must be implemented", pointing into the standard library's definition of the trait, appears for the arithmetic traits that cannot be derived: there is no annotation to suggest, because the implementation has to be written. The string family is identified by its own lines - a label saying "+ cannot be used to concatenate two &str strings" and a note that "string concatenation requires an owned String on the left", with a help that inserts .to_owned() on the left operand. And the built-in families, the shift and the remainder, carry no missing-implementation note at all: there is no type definition in your code to point at, and that absence is the sign that the fix is a different type, not a new implementation.
The first failure: equality on a struct without PartialEq
Point derives Debug only. The comparison a == b needs PartialEq, and nothing has provided it:
#[derive(Debug)]
struct Point { x: i32, y: i32 }
fn main() {
let a = Point { x: 1, y: 2 };
let b = Point { x: 1, y: 2 };
println!("{}", a == b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fail_eq_struct.rs -o fail_eq_struct
error[E0369]: binary operation `==` cannot be applied to type `Point`
--> fail_eq_struct.rs:6:22
|
6 | println!("{}", a == b);
| - ^^ - Point
| |
| Point
|
note: an implementation of `PartialEq` might be missing for `Point`
--> fail_eq_struct.rs:2:1
|
2 | struct Point { x: i32, y: i32 }
| ^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Point` with `#[derive(PartialEq)]`
|
2 + #[derive(PartialEq)]
3 | struct Point { x: i32, y: i32 }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
The note names the missing trait and the help offers the derive. Derived equality on a struct compares every field, which is what comparing two points means, so the fix takes the help:
#[derive(Debug, PartialEq)]
struct Point { x: i32, y: i32 }
fn main() {
let a = Point { x: 1, y: 2 };
let b = Point { x: 1, y: 2 };
println!("{}", a == b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fix_eq_struct.rs -o fix_eq_struct
$ ./fix_eq_struct
true
Point { x: 1, y: 2 } Point { x: 1, y: 2 }
The output is true, then the two points. When field-by-field equality is not the meaning intended - two records that count as the same when one identifying field matches, say - write impl PartialEq by hand and compare only those fields instead of deriving; the operator works the same either way, because both routes provide the one trait.
The second failure: inequality on an enum without PartialEq
The same missing trait on a different kind of type. Light is an enum with no derives, and != is PartialEq's other operator - providing the trait provides both, and lacking it removes both:
#[derive(Debug)]
enum Light { Red, Green }
fn main() {
let a = Light::Red;
let b = Light::Green;
println!("{}", a != b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fail_eq_enum.rs -o fail_eq_enum
error[E0369]: binary operation `!=` cannot be applied to type `Light`
--> fail_eq_enum.rs:6:22
|
6 | println!("{}", a != b);
| - ^^ - Light
| |
| Light
|
note: an implementation of `PartialEq` might be missing for `Light`
--> fail_eq_enum.rs:2:1
|
2 | enum Light { Red, Green }
| ^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Light` with `#[derive(PartialEq)]`
|
2 + #[derive(PartialEq)]
3 | enum Light { Red, Green }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
Derived equality on an enum means the same variant, with equal fields where a variant carries any - here the variants carry nothing, so equality is variant identity alone. The fix is the same derive:
#[derive(Debug, PartialEq)]
enum Light { Red, Green }
fn main() {
let a = Light::Red;
let b = Light::Green;
println!("{}", a != b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fix_eq_enum.rs -o fix_eq_enum
$ ./fix_eq_enum
true
Red Green
The output is true - Red is not Green - then the two variants. A common way this family appears is a comparison added long after the type was defined: the type worked everywhere it was constructed and printed, and the first == or != written against it is the first code that ever needed the trait.
The third failure: addition on a struct without Add
Vec2 is a pair of coordinates, and adding two of them component by component is a natural operation - but natural is not implemented. No Add implementation exists for Vec2:
#[derive(Debug)]
struct Vec2 { x: i32, y: i32 }
fn main() {
let a = Vec2 { x: 1, y: 2 };
let b = Vec2 { x: 10, y: 20 };
let c = a + b;
println!("{:?}", c);
}
$ rustc --edition 2021 fail_add_struct.rs -o fail_add_struct
error[E0369]: cannot add `Vec2` to `Vec2`
--> fail_add_struct.rs:6:15
|
6 | let c = a + b;
| - ^ - Vec2
| |
| Vec2
|
note: an implementation of `Add` might be missing for `Vec2`
--> fail_add_struct.rs:2:1
|
2 | struct Vec2 { x: i32, y: i32 }
| ^^^^^^^^^^^ must implement `Add`
note: the trait `Add` must be implemented
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:76:1
|
76 | pub const trait Add<Rhs = Self> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
There is no derive help here, because Add cannot be derived - what addition means for a type is a decision, and the second note points at the trait definition itself. The fix writes the implementation, stating the result type and adding each field:
use std::ops::Add;
#[derive(Debug)]
struct Vec2 { x: i32, y: i32 }
impl Add for Vec2 {
type Output = Self;
fn add(self, other: Self) -> Self { Vec2 { x: self.x + other.x, y: self.y + other.y } }
}
fn main() {
let a = Vec2 { x: 1, y: 2 };
let b = Vec2 { x: 10, y: 20 };
let c = a + b;
println!("{} {}", c.x, c.y);
}
$ rustc --edition 2021 fix_add_struct.rs -o fix_add_struct
$ ./fix_add_struct
11 22
The output is 11 22, the component sums. Note that this add takes both operands by value, so a and b are moved into the operation - the standard pattern for small copyable data held in fields, and a reason to implement Add for references as well when the operands must survive the addition in larger code.
The fourth failure: a left shift on an f32
This is the error codes index's own erroneous example. Shifting is an integer operation - the Shl trait is implemented for integer types - and an f32 is not one of them:
fn main() {
let x = 12f32;
println!("{}", x << 2);
}
$ rustc --edition 2021 fail_shl_f32.rs -o fail_shl_f32
error[E0369]: no implementation for `f32 << {integer}`
--> fail_shl_f32.rs:3:22
|
3 | println!("{}", x << 2);
| - ^^ - {integer}
| |
| f32
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
The header's form differs from the custom-type families: "no implementation for f32 << {integer}", with no missing-implementation note and no help, because there is no definition of yours to annotate and no implementation you may add to f32. The index's fix is a type change - the same shift on a u32, a type that implements Shl:
fn main() {
let x = 12u32;
println!("{}", x << 2);
}
$ rustc --edition 2021 fix_shl_f32.rs -o fix_shl_f32
$ ./fix_shl_f32
48
The output is 48, twelve shifted left by two places. When this family appears on a value that is genuinely fractional elsewhere in the program, the shift was almost certainly the wrong operation to begin with - multiplying by a power of two is the floating-point operation with the effect a shift has on integers - so read the intent before reaching for the cast.
The fifth failure: a less-than comparison on a struct without PartialOrd
Score wraps a single number, and comparing two scores feels like comparing two numbers - but the comparison runs on Score, and Score has provided no ordering:
#[derive(Debug)]
struct Score { points: i32 }
fn main() {
let a = Score { points: 10 };
let b = Score { points: 20 };
println!("{}", a < b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fail_lt_struct.rs -o fail_lt_struct
error[E0369]: binary operation `<` cannot be applied to type `Score`
--> fail_lt_struct.rs:6:22
|
6 | println!("{}", a < b);
| - ^ - Score
| |
| Score
|
note: an implementation of `PartialOrd` might be missing for `Score`
--> fail_lt_struct.rs:2:1
|
2 | struct Score { points: i32 }
| ^^^^^^^^^^^^ must implement `PartialOrd`
help: consider annotating `Score` with `#[derive(PartialEq, PartialOrd)]`
|
2 + #[derive(PartialEq, PartialOrd)]
3 | struct Score { points: i32 }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
The help offers two traits, not one: PartialOrd requires PartialEq, so a type cannot be ordered without also being equatable, and the derive supplies both together. Derived ordering on a struct is lexicographic by field declaration order - with one field, it is that field's order:
#[derive(Debug, PartialEq, PartialOrd)]
struct Score { points: i32 }
fn main() {
let a = Score { points: 10 };
let b = Score { points: 20 };
println!("{}", a < b);
println!("{:?} {:?}", a, b);
}
$ rustc --edition 2021 fix_lt_struct.rs -o fix_lt_struct
$ ./fix_lt_struct
true
Score { points: 10 } Score { points: 20 }
The output is true, then the two scores. On a multi-field struct, check the declaration order before deriving: the first field dominates the ordering, so a type whose fields are declared in display order rather than priority order gets a derived ordering that sorts by the wrong field - reorder the fields, or implement partial_cmp by hand in the order intended.
The sixth failure: multiplication on a struct without Mul
The arithmetic mirror of the third family, with a different trait. Price has no Mul implementation, so scaling or combining two prices with * has nothing to call:
#[derive(Debug)]
struct Price { cents: i32 }
fn main() {
let a = Price { cents: 300 };
let b = Price { cents: 2 };
let c = a * b;
println!("{:?}", c);
}
$ rustc --edition 2021 fail_mul_struct.rs -o fail_mul_struct
error[E0369]: cannot multiply `Price` by `Price`
--> fail_mul_struct.rs:6:15
|
6 | let c = a * b;
| - ^ - Price
| |
| Price
|
note: an implementation of `Mul` might be missing for `Price`
--> fail_mul_struct.rs:2:1
|
2 | struct Price { cents: i32 }
| ^^^^^^^^^^^^ must implement `Mul`
note: the trait `Mul` must be implemented
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:322:1
|
322 | pub const trait Mul<Rhs = Self> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
The fix has the same shape as the Add fix - import the trait, state Output, write the one method:
use std::ops::Mul;
#[derive(Debug)]
struct Price { cents: i32 }
impl Mul for Price {
type Output = Self;
fn mul(self, other: Self) -> Self { Price { cents: self.cents * other.cents } }
}
fn main() {
let a = Price { cents: 300 };
let b = Price { cents: 2 };
let c = a * b;
println!("{}", c.cents);
}
$ rustc --edition 2021 fix_mul_struct.rs -o fix_mul_struct
$ ./fix_mul_struct
600
The output is 600. Before writing this implementation in real code, ask whether multiplying two values of the type means anything: a price times a price is rarely a price, where a price times a quantity often is - and that second operation is impl Mul<i32> for Price, the same trait with a different right-hand type, which is exactly the flexibility the trait's Rhs parameter exists for. An operator that would mislead its readers is better left unimplemented, with a named method in its place.
The seventh failure: concatenation of two string references
+ on strings is concatenation, and the error codes index states its condition directly: concatenation appends the string on the right to the string on the left and may require reallocation, which requires ownership of the string on the left. Two &str values provide no owned string on either side:
fn main() {
let a = "hello ";
let b = "world";
let c = a + b;
println!("{}", c);
}
$ rustc --edition 2021 fail_str_plus_str.rs -o fail_str_plus_str
error[E0369]: cannot add `&str` to `&str`
--> fail_str_plus_str.rs:4:15
|
4 | let c = a + b;
| - ^ - &str
| | |
| | `+` cannot be used to concatenate two `&str` strings
| &str
|
= note: string concatenation requires an owned `String` on the left
help: create an owned `String` from a string reference
|
4 | let c = a.to_owned() + b;
| +++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
This diagnostic is the most explicit in the guide: the label states the rule, the note restates it as a requirement, and the help performs it. String implements Add<&str>, so with an owned string on the left, the right side can stay a reference - which is also why the working form, String + &str, allocates only for the left string's growth. The fix takes the help:
fn main() {
let a = "hello ";
let b = "world";
let c = a.to_owned() + b;
println!("{}", c);
}
$ rustc --edition 2021 fix_str_plus_str.rs -o fix_str_plus_str
$ ./fix_str_plus_str
hello world
The output is hello world. Compare the first trap below: with an owned String already on the left and a String on the right, the code is one borrow away from working, and the compiler reports that as a type mismatch, E0308, not as E0369 - the implementation exists there, and only its right-hand type was missed.
The eighth failure: a remainder on bool values
The built-in counterpart of the fourth family. bool implements logical operations, not arithmetic ones, so % between two bools has no Rem implementation to call:
fn main() {
let a = true;
let b = false;
println!("{}", a % b);
}
$ rustc --edition 2021 fail_rem_bool.rs -o fail_rem_bool
error[E0369]: cannot calculate the remainder of `bool` divided by `bool`
--> fail_rem_bool.rs:4:22
|
4 | println!("{}", a % b);
| - ^ - bool
| |
| bool
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.
As with the shift family, there is no note and no help: bool is not your type, and no remainder on bools exists to derive or implement. A remainder is a question about integers, so the fix states the operation on integers - here, the remainder the program's numbers actually called for:
fn main() {
let a = 10;
let b = 3;
println!("{}", a % b);
}
$ rustc --edition 2021 fix_rem_bool.rs -o fix_rem_bool
$ ./fix_rem_bool
1
The output is 1, the remainder of ten divided by three. In real code this family usually means a value's type drifted - a flag standing where a count was expected, often after a variable was reused for a different purpose - so trace where the bools came from before rewriting the expression; if the values truly are conditions, the operation wanted was a logical one, && or ||, which bool does implement and which was never an E0369 candidate at all.
The fixes in reach order
- The note says an implementation of
PartialEqmight be missing and the help offers a derive: add#[derive(PartialEq)]- field-by-field on a struct, same-variant on an enum - or implementPartialEqby hand where equality means fewer than all the fields. - The note says
PartialOrdmight be missing and the help offers#[derive(PartialEq, PartialOrd)]: take both derives together, after checking that field declaration order is the ordering intended. - The note says an implementation of
Add,Mul, or anotherstd::opsarithmetic trait might be missing for your own type, with a second note pointing at the trait: write theimpl, withtype Outputand the one method - there is no derive for arithmetic. - The header reads "no implementation for
f32 << ..." or names another built-in type, with no note and no help: the type omits the operation - change the value to a type that implements it, or replace the operation with the one that type supports. - The label says
+cannot concatenate two&strstrings and the note requires an ownedStringon the left: put one there, withto_owned(),String::from, or an existingString- the right side stays a reference. - The header names
boolon both sides of an arithmetic operator: the values are the wrong kind for the operation - use the integers the remainder or arithmetic was about, or a logical operator if the values really are conditions.
Traps
- Adding a
Stringto aStringis E0308, not E0369.Stringdoes implementAdd- for a&stron the right - so a second ownedStringon the right fails as an ordinary type mismatch against that implementation, with a borrow as the fix:
fn main() {
let a = String::from("hello ");
let b = String::from("world");
let c = a + b;
println!("{}", c);
}
$ rustc --edition 2021 fail_string_plus_string.rs -o fail_string_plus_string
error[E0308]: mismatched types
--> fail_string_plus_string.rs:4:17
|
4 | let c = a + b;
| ^ expected `&str`, found `String`
|
help: consider borrowing here
|
4 | let c = a + &b;
| +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
&str, found String" - the error codes index defines E0308 as an expected type that did not match the received type. The boundary is exact: no implementation for the left type at all is E0369 (the seventh family, where the left side was a &str); an implementation that exists for a different right-hand type is E0308. The fix, a + &b, also leaves b alive after the concatenation, where moving it in would not have.
- Adding mixed numeric types is E0277, not E0369. i32 implements Add - for i32. For an f64 on the right there is no implementation, and because the trait itself is implemented in other forms, the compiler reports the unsatisfied bound rather than the binary-operation code:fn main() {
let a = 1i32;
let b = 1.0f64;
println!("{}", a + b);
}
$ rustc --edition 2021 probe_mixed_numeric.rs -o probe_mixed_numeric
error[E0277]: cannot add `f64` to `i32`
--> probe_mixed_numeric.rs:4:22
|
4 | println!("{}", a + b);
| ^ no implementation for `i32 + f64`
|
= help: the trait `Add<f64>` is not implemented for `i32`
help: the following other types implement trait `Add<Rhs>`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:98:9
|
98 | const impl Add for $t {
| ^^^^^^^^^^^^^^^^^^^^^ `i32` implements `Add`
...
113 | add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
| ---------------------------------------------------------------------------------- in this macro invocation
|
::: /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/internal_macros.rs:22:9
|
22 | const impl $imp<$u> for &$t {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&i32` implements `Add<i32>`
...
33 | const impl $imp<&$u> for $t {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `i32` implements `Add<&i32>`
...
44 | const impl $imp<&$u> for &$t {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&i32` implements `Add`
= note: this error originates in the macro `add_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
The header's first line looks like an E0369 header - "cannot add f64 to i32" - and only the code in brackets separates them. The error codes index defines E0277 as using a type which doesn't implement some trait in a place which expected that trait. Rust performs no implicit numeric conversion, so the fix is an explicit one: convert one operand to the other's type, a as f64 + b or a + b as i32, choosing the direction by which type the result needs to be - the two directions are not interchangeable, since one of them discards the fraction.
- Summing a custom type with an iterator's sum is E0277, not E0369. No + operator appears in the code at all - sum requires the Sum trait for the result type over the iterator's item type, and i32 has no such implementation over &Item:
#[derive(Debug)]
struct Item { price: i32 }
fn main() {
let items = vec![Item { price: 10 }, Item { price: 20 }];
let total: i32 = items.iter().sum();
println!("{}", total);
}
$ rustc --edition 2021 probe_iter_sum.rs -o probe_iter_sum
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `&Item`
--> probe_iter_sum.rs:5:35
|
5 | let total: i32 = items.iter().sum();
| ^^^ value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=&Item>`
|
= help: the trait `Sum<&Item>` is not implemented for `i32`
help: `i32` implements trait `Sum<A>`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/accum.rs:50:9
|
50 | impl Sum for $a {
| ^^^^^^^^^^^^^^^ `Sum`
...
72 | impl<'a> Sum<&'a $a> for $a {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Sum<&i32>`
...
206 | integer_sum_product! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
| ---------------------------------------------------------------------------- in this macro invocation
note: the method call chain might not have had the expected associated types
--> probe_iter_sum.rs:5:28
|
4 | let items = vec![Item { price: 10 }, Item { price: 20 }];
| -------------------------------------------- this expression has type `Vec<Item>`
5 | let total: i32 = items.iter().sum();
| ^^^^^^ `Iterator::Item` is `&Item` here
note: required by a bound in `std::iter::Iterator::sum`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:3668:12
|
3665 | fn sum<S>(self) -> S
| --- required by a bound in this associated function
...
3668 | S: [const] Sum<Self::Item>,
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::sum`
= note: this error originates in the macro `integer_sum_product` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
The header names the missing trait - "Sum<&Item> is not implemented for i32" - and there is no binary operation in the failing line to fix. The working form extracts the numbers first, items.iter().map(|item| item.price).sum::<i32>(), summing a type Sum is implemented for; implementing Add on Item alone would not have fixed this one, because sum asks for Sum, not for Add.
- A unary operator on a custom type is E0600, not E0369. The minus in -a has one operand, not two, and the unary code covers it - Neg might be missing exactly as Add was, under a different error number:
#[derive(Debug)]
struct V { x: i32 }
fn main() {
let a = V { x: 5 };
println!("{:?}", -a);
}
$ rustc --edition 2021 probe_unary_neg_struct.rs -o probe_unary_neg_struct
error[E0600]: cannot apply unary operator `-` to type `V`
--> probe_unary_neg_struct.rs:5:22
|
5 | println!("{:?}", -a);
| ^^ cannot apply unary operator `-`
|
note: an implementation of `Neg` might be missing for `V`
--> probe_unary_neg_struct.rs:2:1
|
2 | struct V { x: i32 }
| ^^^^^^^^ 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 header reads "cannot apply unary operator -" and the error codes index defines E0600 as an unary operator used on a type which doesn't implement it. The fix mirrors the binary arithmetic families - implement the named trait, here Neg - and the code in brackets is the only thing that tells a one-operand failure from a two-operand one before the header is read.
Decision summary
- The header says "binary operation
==/!=cannot be applied" under E0369, with aPartialEqnote: the type has no equality - derive it, or implement it on the fields that define sameness. - The header says "binary operation
<cannot be applied" under E0369, with aPartialOrdnote: the type has no ordering - derivePartialEqandPartialOrdtogether, in field order, or implementpartial_cmp. - The header says "cannot add / multiply" your own type under E0369, with a note pointing at your definition and at the trait: write the
std::opsimplementation - arithmetic traits have no derive. - The header says "no implementation for
f32 << ..." under E0369, with no note: a built-in type omits the operation - change the type or the operation. - The header says "cannot add
&strto&str" under E0369, with the owned-Stringnote: put an ownedStringon the left, withto_owned()orString::from. - The header says "cannot calculate the remainder of
bool" under E0369: arithmetic on a non-arithmetic type - use integers, or a logical operator if the values are conditions. - The header says "mismatched types" and "expected
&str, foundString" (E0308): the implementation exists and the right operand missed its type - borrow it. - The header says "cannot add
f64toi32" under E0277: the trait exists for other operand types - convert one operand explicitly; no implicit numeric conversion happens. - The header says a value "cannot be made by summing" under E0277:
sumneedsSum, not the operator - map to a summable type first, or implementSum. - The header says "cannot apply unary operator" (E0600): one operand, not two - implement the unary trait it names, such as
NegorNot.
Checklist
- The operator in the header converted to its trait before any code changes:
PartialEqfor==/!=,PartialOrdfor</>/<=/>=, the matchingstd::opstrait for arithmetic. - The missing-implementation note read for which case applies: a derive offered (comparisons), an implementation to write (arithmetic on your own type), or no note at all (a built-in type that omits the operation).
- Derived comparisons checked for meaning: equality is all fields, ordering is field declaration order - and a manual implementation written instead where either meaning is wrong.
- Arithmetic implementations given an
Outputtype that is honest about the result, and a right-hand type that matches how the operation is actually used. - String concatenation given an owned
Stringon the left, with the right side left as a&str- and an existing right-handStringborrowed, not moved. - Mixed numeric operands converted explicitly, in the direction whose result type the code needs.
- The neighboring codes separated by the code in brackets, not by the header's first line: E0308 where an implementation exists, E0277 where a bound fails, E0600 where the operator is unary.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
Sources
- Rust, Error codes index: E0369 - the definition of the error: a binary operation attempted on a type which doesn't support it, fixed by checking that the type implements the binary operation, with the
<<on anf32example, the note that most operators can be overloaded by implementing traits fromstd::ops, and the rule that string concatenation requires ownership of the string on the left, withto_owned()for a literal. - Rust std::ops module - overloadable operators: implementing the traits in this module allows overloading certain operators, only operators backed by traits can be overloaded, and the trait behind each operator, including
Add,Mul,Rem, andShl. - The Rust Programming Language, Appendix B: Operators and Symbols - the operator table mapping each operator to its overloadable trait:
PartialEqfor==and!=,PartialOrdfor<,<=,>, and>=,Addfor+,Mulfor*,Shlfor<<, andRemfor%. - Rust std::cmp, PartialEq trait - implementing
PartialEqprovides the==and!=operators; derived on structs, instances are equal if all fields are equal, and on enums, if they are the same variant and all fields are equal. - Rust std::cmp, PartialOrd trait -
PartialOrdprovides the<,<=,>, and>=operators, requiresPartialEq, and when derived on structs produces a lexicographic ordering based on the declaration order of the struct's members. - Rust, Error codes index: E0308 - the neighboring mismatch error: an expected type did not match the received type, as when an existing
Addimplementation expects a&strand receives aString. - Rust, Error codes index: E0277 - the neighboring trait-bound error: a type used which doesn't implement some trait in a place which expected that trait, covering mixed-operand operators and iterator
sum. - Rust, Error codes index: E0600 - the neighboring unary error: an unary operator used on a type which doesn't implement it.
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.