skip to content

In Rust, why does rustc reject `n.send(..)` with E0599 when `EmailNotifier` implements the `Notifier` trait, and how do you fix it?

level: middleimportance: should knowfreq 45%

answer

  1. implemented is not visible
  2. method lookup uses visible traits
  3. import the trait, not the method
  4. use path::Trait as _

basics

~20 s

Trait methods are only candidates for method-call syntax when the trait itself is in scope. Importing the type is not enough; add use crate::notify::Notifier (or use ... as _) in the calling module, or call Notifier::send(&n, ..) by path.

solid answer

~50 s

Method lookup for `n.send(..)` searches the type's inherent methods and then the methods of **visible** traits implemented by the type. If the calling module imports only `EmailNotifier`, the impl exists but `Notifier` is not in scope, so rustc reports E0599, `no method named `send` found`, with the help line `items from traits can only be used if the trait is in scope` and a suggested `use crate::notify::Notifier;`. The fix is to import the trait in that module, or write `use crate::notify::Notifier as _;` to enable its methods without binding the name, or call `crate::notify::Notifier::send(&n, ..)` by path. The rule exists because any crate can add methods to existing types through its own traits; requiring an import keeps those methods opt-in and limits name clashes. Traits in the standard prelude, such as `Clone` or `Iterator`, are in scope in every module by default.

code

rust · 20 lines
rust
mod notify {
    pub trait Notifier {
        fn send(&self, msg: &str) -> String;
    }

    pub struct EmailNotifier;

    impl Notifier for EmailNotifier {
        fn send(&self, msg: &str) -> String {
            format!("email: {msg}")
        }
    }
}

use notify::EmailNotifier;

fn main() {
    let n = EmailNotifier;
    println!("{}", n.send("deploy finished"));
}

go deeper

for a junior

Recall that calling a trait method needs the trait itself imported, and read rustc's help line, which names the use path to add.

for a middle

Explain that method lookup only considers visible traits, so an implemented but unimported trait yields E0599, and list the import, as _ and path-call fixes.

for a senior

Connect the rule to extension traits: imports keep foreign methods opt-in per module and confine name clashes, which matters when adding dependencies or designing a library prelude.

for a principal

Decide how a library exposes its extension traits, such as a prelude module to glob-import, and weigh that convenience against the clash risk it imports into every user module.

## The error A team splits its notification code into a module `notify` that defines a `Notifier` trait and an `EmailNotifier` type implementing it. Code elsewhere imports the type and calls the method: ```rust use notify::EmailNotifier; let n = EmailNotifier; n.send("deploy finished"); // error[E0599] ``` rustc 1.99.1 answers with E0599, `no method named `send` found for struct `EmailNotifier` in the current scope`, then adds two lines that explain everything: `items from traits can only be used if the trait is in scope`, and a suggestion to add `use crate::notify::Notifier;`. ## Why implementing is not enough When you write `value.method()`, rustc searches, for each candidate receiver type, the type's **inherent methods** and then the methods provided by **visible traits** implemented by that type (the Rust Reference's method-call rules). "Visible" means the trait is in scope at the call site: imported with `use`, defined in the same module, or part of the prelude. So two separate facts must both hold for `n.send(..)`: 1. `EmailNotifier` implements `Notifier` somewhere in the crate graph. 2. `Notifier` is in scope in the module that makes the call. The first is true here; the second is not. Importing the struct does not import its traits, because an implementation is not owned by the type. Any crate may define its own trait and implement it for `EmailNotifier`, or for `String` or `Vec<T>`. ## Why Rust is designed this way Traits are Rust's way of adding methods to types you did not write. A crate can declare `trait RetryExt` and implement it for a type from another crate (within the coherence rules covered elsewhere). If every implemented trait applied everywhere automatically: - adding a dependency could add methods to types throughout your code without you asking; - two unrelated traits that both define `send` for the same type would collide in every module, not just in modules that use both; - reading `n.send(..)` would not tell you which trait to look up. Requiring the trait to be in scope keeps these **extension methods** opt-in per module. Name clashes can then appear only where both traits are imported, and the `use` line documents where the method comes from. ## Ways to fix it | Fix | Example | When to choose it | |---|---|---| | Import the trait | `use crate::notify::Notifier;` | the usual fix; the name is also usable in bounds | | Underscore import | `use crate::notify::Notifier as _;` | you want the methods but the name `Notifier` would clash or is unused | | Call by path | `crate::notify::Notifier::send(&n, "deploy finished")` | a one-off call, or to make the trait explicit | | Re-export from a prelude module | `pub use crate::notify::Notifier;` in your crate's own prelude | many modules need the same traits | The Rust Reference describes `use path as _` as importing an item without binding a name, and names importing a trait for its methods as the typical use. ## Where traits are already in scope - The **standard prelude** brings common traits into every module, including `Clone`, `Iterator`, `ToString`, `Into` and `From`. The 2021 edition prelude added `TryFrom`, `TryInto` and `FromIterator`; how preludes are assembled is covered with paths and imports. - Inside the module that defines the trait, it is in scope without a `use`. - In generic code, a bound such as `T: Notifier` lets you call `Notifier` methods on `T`; the bound has to name the trait, so it is reachable there anyway. ## A worked repair Starting from the failing snippet below, the repair takes three small steps: 1. Read the `help:` line: `trait `Notifier` which provides `send` is implemented but not in scope`. 2. Decide whether the module also needs the name `Notifier`, for example in a bound such as `fn alert(n: &impl Notifier)`. If it does, add `use crate::notify::Notifier;`. 3. If it only calls methods, `use crate::notify::Notifier as _;` is enough; with it the program prints `email: deploy finished`. Nothing about `EmailNotifier` or its impl changes. The fix lives entirely in the calling module, because visibility is decided per module, not per type. ## Diagnosing it quickly - Read the `help:` lines: rustc names the trait that provides the method and the exact `use` path. - If several traits provide the method, rustc lists them; import the one you meant, and be aware that importing two can turn the call into an E0034 ambiguity. - In a library, if users must import a trait for its methods, document it, or export it from a prelude module they can glob-import.

  • What does `use crate::notify::Notifier as _;` do differently from a plain import?
    It brings the trait's methods into scope for method-call syntax without binding the name `Notifier` in the module. That avoids a clash with another item called `Notifier`, and it can never shadow or conflict with a name you use elsewhere. You cannot then use `Notifier` in a bound or a path without importing it normally.
  • Why can you call `clone()` or `to_string()` without importing anything?
    `Clone` and `ToString` are in the standard prelude, which every module gets implicitly, so their methods are always visible. Only traits outside the prelude, such as `std::io::Write` or `std::fmt::Write`, have to be imported before their methods resolve.
  • Can you call `send` without importing `Notifier` at all?
    Yes, by calling it as a function through the trait's path: `crate::notify::Notifier::send(&n, "deploy finished")`. Path calls do not depend on the trait being in scope, which also makes them a quick way to confirm which trait a method belongs to.

  • Importing a type also imports every trait it implements.
  • E0599 here means EmailNotifier does not implement Notifier.
  • You import a trait method with use crate::notify::Notifier::send.
  • Trait methods are in scope everywhere once the impl exists in the crate.
  • use Trait as _ hides the trait's methods instead of enabling them.