Rust Error E0405: Cannot Find Trait in This Scope
E0405 means you named a trait that Rust can’t see from that spot. The classic case: you write impl Display for Point to make your struct printable, but you never imported Display. A typo in the trait’s name, or a trait that lives in another module, gives the same error. It’s one of the easiest errors to fix, because rustc usually prints the exact use line you need. Unlike E0412 (“cannot find type”), which became E0425 in Rust 1.93, this code hasn’t changed: rustc 1.97 still prints E0405 for traits. It’s part of the compiler errors guide.
The error message
Section titled “The error message”error[E0405]: cannot find trait `Display` in this scope --> src/main.rs:6:6 |6 | impl Display for Point { // ❌ `Display` was never imported | ^^^^^^^ not found in this scope |help: consider importing this trait |1 + use std::fmt::Display; |This is the program that produces it:
struct Point { x: i32, y: i32,}
impl Display for Point { // ❌ `Display` was never imported fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "({}, {})", self.x, self.y) }}
fn main() { let p = Point { x: 3, y: 4 }; println!("{}", p);}What it means
Section titled “What it means”The ^^^^^^^ points at Display on line 6, with the label “not found in this scope”. Rust looked at every trait visible from there: the ones you defined in this module, the ones your use lines bring in, and the small set in the prelude. None of them is called Display.
The help: tells you where it is: use std::fmt::Display;. The 1 + means “add this as a new line 1”, at the top of the file.
Why is Display missing when println! uses it all the time? The prelude, the list of names every Rust file gets for free, includes Clone, Iterator, From, Default and other everyday traits, but not Display, Error, Hash or Write. Those live in their modules (std::fmt, std::error, std::hash, std::io), and you import them when you implement them.
It’s like a recipe that says “use the Display pan”. The cook knows the pans on the stove, but this one is in a cupboard, and the recipe never said which one.
Why Rust does this
Section titled “Why Rust does this”Rust never searches the whole standard library, or your whole project, for a name. It only looks at what’s in scope: your own items, your use lines and the prelude. That keeps names predictable. There are several traits called Write and Error in std alone, and a crate you add later could bring more. If Rust picked one for you, adding a dependency could quietly change what your code means.
So you say which trait you mean, once, at the top of the file. The help line does the searching for you and suggests the trait it found. See Modules for how use works, and Common Traits for Display.
How to fix it
Section titled “How to fix it”Fix 1: Add the use line rustc suggests
Section titled “Fix 1: Add the use line rustc suggests”Choose this when the help says consider importing this trait, which is most of the time. Copy the line to the top of the file:
use std::fmt::Display;
struct Point { x: i32, y: i32,}
impl Display for Point { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "({}, {})", self.x, self.y) }}
fn main() { let p = Point { x: 3, y: 4 }; println!("{}", p); println!("Ferris is at {}", Point { x: -1, y: 7 });}Output:
(3, 4)Ferris is at (-1, 7)If rustc suggests several imports, read them before you choose. They’re different traits that happen to share a name, like std::fmt::Write and std::io::Write.
Fix 2: Import the module and write fmt::Display
Section titled “Fix 2: Import the module and write fmt::Display”Choose this when you’re implementing Display, because you need Formatter and Result from the same module anyway. Import std::fmt once and write fmt:: in front of all three. This is the style you’ll see most in real Rust code:
use std::fmt;
struct Point { x: i32, y: i32,}
impl fmt::Display for Point { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "({}, {})", self.x, self.y) }}
fn main() { let p = Point { x: 3, y: 4 }; let label = p.to_string(); println!("{} has {} characters", label, label.len());}Output:
(3, 4) has 6 charactersImplementing Display also gives you .to_string() for free. You can skip the use completely and write impl std::fmt::Display for Point; that’s fine for a one-off.
Fix 3: Fix the spelling of the trait’s name
Section titled “Fix 3: Fix the spelling of the trait’s name”Choose this when rustc says a trait with a similar name exists. Trait names are case-sensitive, so shape, Shap and Shape are three different names:
error[E0405]: cannot find trait `Shap` in this scope --> src/main.rs:9:6 |1 | trait Shape { | ----------- similarly named trait `Shape` defined here...9 | impl Shap for Square { // ❌ typo: Shap | ^^^^ |help: a trait with a similar name exists |9 | impl Shape for Square { // ❌ typo: Shap | +The broken program:
trait Shape { fn area(&self) -> f64;}
struct Square { side: f64,}
impl Shap for Square { // ❌ typo: Shap fn area(&self) -> f64 { self.side * self.side }}
fn main() { let s = Square { side: 3.0 }; println!("{}", s.area());}The + under line 9 shows the missing letter. Add it:
trait Shape { fn area(&self) -> f64;}
struct Square { side: f64,}
impl Shape for Square { fn area(&self) -> f64 { self.side * self.side }}
fn main() { let s = Square { side: 3.0 }; println!("{}", s.area());}Output:
9If there’s no similar name and no import suggestion, the trait doesn’t exist yet. Define it with trait Shape { ... } first. See Traits.
Fix 4: Import your own trait from its module
Section titled “Fix 4: Import your own trait from its module”Choose this when the trait is yours but lives in another module. Each module has its own scope, so a trait in mod shapes isn’t visible at the top of main.rs until you import it:
error[E0405]: cannot find trait `Shape` in this scope --> src/main.rs:11:6 |11 | impl Shape for Square { // ❌ `Shape` lives in `mod shapes` | ^^^^^ not found in this scope |help: consider importing this trait | 1 + use crate::shapes::Shape; |The broken program:
mod shapes { pub trait Shape { fn area(&self) -> f64; }}
struct Square { side: f64,}
impl Shape for Square { // ❌ `Shape` lives in `mod shapes` fn area(&self) -> f64 { self.side * self.side }}
fn main() { println!("{}", Square { side: 2.0 }.area());}Add the use line, and check that the trait is pub. Without pub, the import itself fails with E0603, “trait is private”:
mod shapes { pub trait Shape { fn area(&self) -> f64; }}
use crate::shapes::Shape;
struct Square { side: f64,}
impl Shape for Square { fn area(&self) -> f64 { self.side * self.side }}
fn main() { println!("{}", Square { side: 2.0 }.area());}Output:
4The same goes for traits from crates: impl Serialize for User needs use serde::Serialize; first. See Modules.
Where you’ll usually see it
Section titled “Where you’ll usually see it”Box<dyn Error> in main or a helper. fn run() -> Result<(), Box<dyn Error>> without an import gives cannot find trait `Error` in this scope, with help: consider importing this trait, use std::error::Error;. Fix: add that line, or write Box<dyn std::error::Error>. See Custom Errors.
Trait bounds on generics. fn print_all<T: Display>(items: &[T]) gives the same E0405 for Display, pointing at the bound. Fix: use std::fmt::Display;. See Generics.
Debug gives a different code. impl Debug for Point or T: Debug without an import is not E0405 but E0404: expected trait, found derive macro `Debug` . The prelude has the #[derive(Debug)] macro, but not the Debug trait. Fix: use std::fmt::Debug; (rustc suggests it), or just #[derive(Debug)] if you don’t need a custom format.
Related errors
Section titled “Related errors”- E0425: cannot find a value, function, or (since Rust 1.93) a type in this scope.
- E0412: the old code for “cannot find type”, now reported as E0425.
- E0433: cannot find the first part of a path, like
fmt::Displaywithoutuse std::fmt;. - E0599: no method found, often because the trait that provides the method isn’t imported.
- E0277: the trait was found, but your type doesn’t implement it.
- E0404: a name that exists, but isn’t a trait (like the
Debugmacro), shown above. - All compiler errors: the full list of error pages.
Quick check
Section titled “Quick check”Quick check
1. You write impl Display for Invoice and get E0405. What's missing?
2. Which of these traits can you use without any use line?
3. Your trait Shape is defined in mod shapes, and impl Shape for Square is in main.rs. What do you need?
Score: 0 / 3
Read more
Section titled “Read more”- E0405 — Rust error codes index: the official explanation, with the two fixes: import the trait or define it.
- Implementing a Trait on a Type — The Rust Book: how
impl Trait for Typeworks, and why the trait has to be in scope. std::fmt::Display— standard library docs: the trait from this page, with a fullimpl fmt::Displayexample.- Display — Rust by Example: implementing
Displaystep by step, withuse std::fmt;. std::prelude— standard library docs: the list of traits and types you can use without importing them.