Email me

Rust errors / Names, paths and imports

Rust compiler error · checked with rustc 1.99.0

Rust error E0424: cannot find value `self` in this scope

What it meansThe self keyword was used inside of an associated function without a "self receiver" parameter.

What rustc prints

error[E0424]: cannot find value `self` in this scope
 --> main.rs:9:9
  |
8 |     fn foo() {
  |        --- this function doesn't have a `self` parameter
9 |         self.bar(); // error: `self` value is a keyword only available in
  |         ^^^^ `self` value is a keyword only available in methods with a `self` parameter
  |
  = note: a module named `self` exists in another namespace
help: add a `self` receiver parameter to make the associated `fn` a method
  |
8 |     fn foo(&self) {
  |            +++++

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.

E0424Names, paths and imports
Activein rustc 1.99.0
7related errors linked below

The compiler team's explanation

The self keyword was used inside of an associated function without a "self receiver" parameter.

Erroneous code example:

struct Foo;

impl Foo {
    // `bar` is a method, because it has a receiver parameter.
    fn bar(&self) {}

    // `foo` is not a method, because it has no receiver parameter.
    fn foo() {
        self.bar(); // error: `self` value is a keyword only available in
                    //        methods with a `self` parameter
    }
}

The self keyword can only be used inside methods, which are associated functions (functions defined inside of a trait or impl block) that have a self receiver as its first parameter, like self, &self, &mut self or self: &mut Pin<Self> (this last one is an example of an "arbitrary self type").

Check if the associated function's parameter list should have contained a self receiver for it to be a method, and add it if so. Example:

struct Foo;

impl Foo {
    fn bar(&self) {}

    fn foo(self) { // `foo` is now a method.
        self.bar(); // ok!
    }
}

From rustc --explain E0424 (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 nextE0425: cannot find value in this scopeA 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