Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0277: the trait bound is not satisfied

What it meansA type is used somewhere that needs a trait it does not implement. Printing needs Display or Debug, threads need Send, ? needs a compatible error type, generic functions need whatever their bounds say.

What rustc prints

error[E0277]: the trait bound `i32: Foo` is not satisfied
  --> main.rs:14:15
   |
14 |     some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
   |     --------- ^^^^ the trait `Foo` is not implemented for `i32`
   |     |
   |     required by a bound introduced by this call
   |
help: this trait has no implementations, consider adding one
  --> main.rs:2:1
   |
 2 | trait Foo {
   | ^^^^^^^^^
note: required by a bound in `some_func`
  --> main.rs:7:17
   |
 7 | fn some_func<T: Foo>(foo: T) {
   |                 ^^^ required by this bound in `some_func`

Real 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

  • println!("{}", x) on a struct without Display (use {:?} plus #[derive(Debug)]).
  • Using ? in main or a function that returns ().
  • Using ? on an error type that does not convert into the function's error type.
  • Sending an Rc or RefCell to another thread (they are not Send/Sync).
  • Calling a generic function with a type that misses a bound like Clone, Hash or Ord.

How to fix it

1. Derive the trait

Most standard traits can be derived.

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Point { x: i32, y: i32 }

2. Return a Result from main

Then ? works in main.

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = std::fs::read_to_string("in.txt")?;
    Ok(())
}

3. Use thread-safe types

Arc instead of Rc, Mutex or RwLock instead of RefCell.

use std::sync::{Arc, Mutex};
let shared = Arc::new(Mutex::new(Vec::new()));

4. Implement it yourself

For Display, write the formatting you want.

impl std::fmt::Display for Point {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

Other wordings of this error

the trait bound is not satisfied, the trait `, is not implemented for, doesn't implement `std::fmt::Display`, doesn't implement `Debug`, cannot be sent between threads safely, the size for values of type, the `?` operator can only be used

E0277Types and traits
Activein rustc 1.99.0
7related errors linked below

The compiler team's explanation

You tried to use a type which doesn't implement some trait in a place which expected that trait.

Erroneous code example:

// here we declare the Foo trait with a bar method
trait Foo {
    fn bar(&self);
}

// we now declare a function which takes an object implementing the Foo trait
fn some_func<T: Foo>(foo: T) {
    foo.bar();
}

fn main() {
    // we now call the method with the i32 type, which doesn't implement
    // the Foo trait
    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
}

In order to fix this error, verify that the type you're using does implement the trait. Example:

trait Foo {
    fn bar(&self);
}

// we implement the trait on the i32 type
impl Foo for i32 {
    fn bar(&self) {}
}

fn some_func<T: Foo>(foo: T) {
    foo.bar(); // we can now use this method since i32 implements the
               // Foo trait
}

fn main() {
    some_func(5i32); // ok!
}

Or in a generic context, an erroneous code example would look like:

fn some_func<T>(foo: T) {
    println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
                           //        implemented for the type `T`
}

fn main() {
    // We now call the method with the i32 type,
    // which *does* implement the Debug trait.
    some_func(5i32);
}

Note that the error here is in the definition of the generic function. Although we only call it with a parameter that does implement Debug, the compiler still rejects the function. It must work with all possible input types. In order to make this example compile, we need to restrict the generic type we're accepting:

use std::fmt;

// Restrict the input type to types that implement Debug.
fn some_func<T: fmt::Debug>(foo: T) {
    println!("{:?}", foo);
}

fn main() {
    // Calling the method is still fine, as i32 implements Debug.
    some_func(5i32);

    // This would fail to compile now:
    // struct WithoutDebug;
    // some_func(WithoutDebug);
}

Rust only looks at the signature of the called function, as such it must already specify all requirements that will be used for every type parameter.

From rustc --explain E0277 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0308: mismatched typesA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ