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.
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
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?