Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0401: can't use generic parameters from outer item

What it meansInner items do not inherit the generic parameters from the items they are embedded in.

What rustc prints

error[E0401]: can't use generic parameters from outer item
 --> main.rs:2:15
  |
1 | fn foo<T>(x: T) {
  |        - type parameter from outer item
2 |     fn bar(y: T) { // T is defined in the "outer" function
  |        ---    ^ use of generic parameter from outer item
  |        |
  |        generic parameter used in this inner function
  |
  = note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
  |
2 |     fn bar<T>(y: T) { // T is defined in the "outer" function
  |           +++

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.

E0401Types and traits
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

Inner items do not inherit the generic parameters from the items they are embedded in.

Erroneous code example:

fn foo<T>(x: T) {
    fn bar(y: T) { // T is defined in the "outer" function
        // ..
    }
    bar(x);
}

Nor will this:

fn foo<T>(x: T) {
    type MaybeT = Option<T>;
    // ...
}

Or this:

fn foo<T>(x: T) {
    struct Foo {
        x: T,
    }
    // ...
}

Items nested inside other items are basically just like top-level items, except that they can only be used from the item they are in.

There are a couple of solutions for this.

If the item is a function, you may use a closure:

fn foo<T>(x: T) {
    let bar = |y: T| { // explicit type annotation may not be necessary
        // ..
    };
    bar(x);
}

For a generic item, you can copy over the parameters:

fn foo<T>(x: T) {
    fn bar<T>(y: T) {
        // ..
    }
    bar(x);
}
fn foo<T>(x: T) {
    type MaybeT<T> = Option<T>;
}

Be sure to copy over any bounds as well:

fn foo<T: Copy>(x: T) {
    fn bar<T: Copy>(y: T) {
        // ..
    }
    bar(x);
}
fn foo<T: Copy>(x: T) {
    struct Foo<T: Copy> {
        x: T,
    }
}

This may require additional type hints in the function body.

In case the item is a function inside an impl, defining a private helper function might be easier:

impl<T> Foo<T> {
    pub fn foo(&self, x: T) {
        self.bar(x);
    }

    fn bar(&self, y: T) {
        // ..
    }
}

For default impls in traits, the private helper solution won't work, however closures or copying the parameters should still work.

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