Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0801: invalid generic `self` parameter type: `R`

What it meansThe self parameter in a method has an invalid generic "receiver type".

What rustc prints

error[E0801]: invalid generic `self` parameter type: `R`
 --> main.rs:4:51
  |
4 |     fn foo<R: std::ops::Deref<Target=Self>>(self: R) {}
  |                                                   ^
  |
  = note: type of `self` must not be a method generic parameter type
  = help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `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.

E0801Types and traits
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

The self parameter in a method has an invalid generic "receiver type".

Erroneous code example:

struct Foo;

impl Foo {
    fn foo<R: std::ops::Deref<Target=Self>>(self: R) {}
}

or alternatively,

struct Foo;

impl Foo {
    fn foo(self: impl std::ops::Deref<Target=Self>) {}
}

Methods take a special first parameter, termed self. It's normal to use self, &self or &mut self, which are syntactic sugar for self: Self, self: &Self, and self: &mut Self respectively. But it's also possible to use more sophisticated types of self parameter, for instance std::rc::Rc<Self>. The set of allowable Self types is extensible using the nightly feature Arbitrary self types. This will extend the valid set of Self types to anything which implements std::ops::Deref<Target=Self>, for example Rc<Self>, Box<Self>, or your own smart pointers that do the same.

However, even with that feature, the self type must be concrete. Generic self types are not permitted. Specifically, a self type will be rejected if it is a type parameter defined on the method.

These are OK:

struct Foo;

impl Foo {
    fn foo(self) {}
    fn foo2(self: std::rc::Rc<Self>) {} // or some other similar
        // smart pointer if you enable arbitrary self types and
        // the pointer implements Deref<Target=Self>
}

From rustc --explain E0801 (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