Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0369: binary operation cannot be applied to type
What it meansAn operator like + or == was used on a type that does not implement it.
What rustc prints
error[E0369]: no implementation for `f32 << {integer}`
--> main.rs:6:3
|
6 | x << 2;
| - ^^ - {integer}
| |
| f32Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
- Comparing structs with
==without#[derive(PartialEq)]. - Adding two
&strvalues (useformat!or a String).
How to fix it
1. Derive or implement the operator trait
#[derive(PartialEq)]
struct Id(u32);2. Build a String
let full = format!("{first} {last}");Other wordings of this error
binary operation cannot be applied to type, cannot be applied to type, no implementation for
The compiler team's explanation
A binary operation was attempted on a type which doesn't support it.
Erroneous code example:
let x = 12f32; // error: binary operation `<<` cannot be applied to
// type `f32`
x << 2;
To fix this error, please check that this type implements this binary operation. Example:
let x = 12u32; // the `u32` type does implement it:
// https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
x << 2; // ok!
It is also possible to overload most operators for your own type by implementing traits from std::ops.
String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left. If something should be added to a string literal, move the literal to the heap by allocating it with to_owned() like in "Your text".to_owned().
From rustc --explain E0369 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0367`Drop` impl requires `T: Foo` but the struct it is implemented for does not
- E0368binary assignment operation `<<=` cannot be applied to type `f32`
- E0371the object type `(dyn Baz + 'static)` automatically implements the trait `Bar`
- E0374implementing `CoerceUnsized` requires a field to be coerced
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?