error[E0369]: binary operation == cannot be applied to type
You used an operator such as ==, + or < on a type that doesn’t support it. In Rust, every operator is a trait, and your own types don’t implement them until you say so.
Quick fix: Add #[derive(PartialEq)] for == and !=, or implement the operator trait from std::ops for arithmetic.
Code that triggers E0369
struct Point {
x: i32,
y: i32,
}
fn main() {
let a = Point { x: 1, y: 2 };
let b = Point { x: 1, y: 2 };
if a == b {
println!("same point at {}, {}", a.x, a.y);
}
}What the compiler says
error[E0369]: binary operation `==` cannot be applied to type `Point`
--> src/main.rs:9:10
|
9 | if a == b {
| - ^^ - Point
| |
| Point
|
note: an implementation of `PartialEq` might be missing for `Point`
--> src/main.rs:1:1
|
1 | struct Point {
| ^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Point` with `#[derive(PartialEq)]`
|
1 + #[derive(PartialEq)]
2 | struct Point {
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0369`.Why Rust raises E0369
== calls PartialEq::eq, + calls Add::add, and < calls PartialOrd::partial_cmp. Rust can’t know what equality or addition should mean for your struct, so it implements none of them by default. The compiler’s note points at the struct and names the missing trait.
How to fix E0369
1. Derive PartialEq
For field-by-field comparison, derive it: two Points are equal when all their fields are equal. Add Eq, PartialOrd, Ord or Hash the same way when you need them.
#[derive(PartialEq)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let a = Point { x: 1, y: 2 };
let b = Point { x: 1, y: 2 };
if a == b {
println!("same point at {}, {}", a.x, a.y);
}
}2. Implement the operator trait yourself
Arithmetic operators can’t be derived. Implement the trait from std::ops: here Add makes a + b work for points.
use std::ops::Add;
#[derive(Debug, Clone, Copy)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
fn main() {
let a = Point { x: 1, y: 2 };
let b = Point { x: 3, y: 4 };
println!("{:?}", a + b);
}Related errors
- E0277: type doesn’t implement
std::fmt::Display - E0308: mismatched types
- E0599: no method named
push_strfound for reference&str
Learn the concept behind it
The Structs by Example lesson explains the rules behind E0369 with diagrams and more examples. For the official reference, run rustc --explain E0369 or read the E0369 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.