E0401 Can't Use Generic Parameters Fix
Written November 27, 2026 · Tested with Rust 1.99.1 · 29 min read
- The error: a nested function using the outer type parameter
- First fix: declare the same parameter on the inner item
- Second fix: move the inner function out
- Third fix: use a closure instead of a nested function
Written November 27, 2026
Tested with Rust 1.99.1
What this guide is
E0401 is the error for an inner item that uses a generic parameter it does not declare. Items written inside other items — a function inside a function, a type alias, struct, or enum inside a function, a function inside a method — do not inherit the generic parameters of the item around them. A nested function is its own item, with its own parameter list, and the outer function's T, 'a, or const N is not on that list. Writing the outer parameter's name inside the inner item therefore names a parameter that, from the inner item's side, does not exist, and the compiler stops with E0401, can't use generic parameters from outer item.
The repairs are all ways of giving the inner item a legitimate route to the parameter. Declare the same parameter on the inner item, and pass the value in as an ordinary argument. Move the inner item out to the top level, where it declares its parameters in the usual place. Replace the inner function with a closure, which is not an item at all and can use the outer function's parameters and values directly. Or, inside an impl, turn the inner function into a method, because methods do share the impl's parameters. The sections below take the shapes in order: the plain type parameter case, the three main repairs on it, lifetime and const parameters — where the diagnostic's wording changes and, for const parameters, the compiler's own suggestion misleads — bounds, impl blocks and methods, the other inner items, the things an inner function can still use, and the neighboring errors.
The error: a nested function using the outer type parameter
This program declares a generic function and, inside it, a helper function written against the outer function's type parameter:
fn outer<T>(x: T) {
fn inner(y: T) {
let _ = y;
}
inner(x);
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:2:17
|
1 | fn outer<T>(x: T) {
| - type parameter from outer item
2 | fn inner(y: T) {
| ----- ^ 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 inner<T>(y: T) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
The diagnostic points at both ends of the mistake. One span marks the parameter where the outer function declares it, labeled type parameter from outer item. The other marks the use inside the inner function, labeled use of generic parameter from outer item, with a second label naming the kind of inner item: generic parameter used in this inner function. The note under the spans states the rule the whole guide hangs on: nested items are independent from their parent item for everything except for privacy and name resolution. Independence is the design, not a quirk. The inner function can be called from anywhere the outer function's body can reach, compiled once as its own item, and nothing about the outer call — which concrete type T turned out to be this time — is available to it unless its own signature says so. The help block then offers the first repair: introduce a local generic parameter, fn inner<T>.
First fix: declare the same parameter on the inner item
Following the help block exactly — giving the inner function its own T — repairs the program, and the outer value passes in as an ordinary argument:
fn outer<T>(x: T) {
fn inner<T>(y: T) {
let _ = y;
}
inner(x);
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
done
The program prints done and exits with status 0. Read the repaired signature carefully, because it says something subtler than it looks. The inner T is a new parameter that happens to share a name with the outer one; it is not the outer parameter arriving by another route. The inner function is generic in its own right, and at the call inner(x) the compiler settles the inner T from the argument, which is the outer T's value. That is why this fix works even when the two parameters end up as different types in other calls: each call to the inner function picks its own types from its own arguments. It is also why the bounds have to travel with the parameter, a point a later section takes up: the inner declaration starts empty, with none of the outer parameter's bounds attached.
Second fix: move the inner function out
An inner function that declares its own parameters has no remaining reason to sit inside the outer function, unless the nesting itself is doing organizational work you want to keep. Moving it to the top level changes nothing about how it is called:
fn inner<T>(y: T) {
let _ = y;
}
fn outer<T>(x: T) {
inner(x);
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
done
The program prints done and exits with status 0. This is the same repair as the previous section, finished: once the parameter is declared on the inner item, the item stands on its own, and the top level is where standalone items normally live. Choose between the two forms by intent. Keep the function nested when it is a private helper for this one outer function and you want it invisible everywhere else — nested items can only be used from the item they are in. Move it out when other code could use it too, or when the outer function is getting hard to read. What you cannot do is keep the nesting and skip the declaration: the declaration is the price of the parameter, wherever the item sits.
Third fix: use a closure instead of a nested function
A closure is not an item. It is an expression that produces a value, written in the middle of the outer function's body, and it lives under the opposite rule: it may use the outer function's generic parameters in its signature, and it may capture the outer function's values outright. Rewriting the nested function as a closure needs no new parameter declaration at all:
fn outer<T>(x: T) {
let inner = |y: T| {
let _ = y;
};
inner(x);
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
done
The program prints done and exits with status 0. The closure's parameter is annotated T, the outer function's own parameter, and that is legal here precisely because the closure has no parameter list of its own to keep separate — its types are worked out in the scope where it is written. The closure can go further and skip the parameter entirely, capturing the value itself:
fn outer<T>(x: T) -> T {
let get = || x;
get()
}
fn main() {
println!("{}", outer(42));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. Capture is the thing a function item can never do, and it is the sharpest line between the two forms. Choose the closure when the helper is short, used in this one place, and reads most clearly with the surrounding values in reach. Choose a function — nested with its own parameters, or moved out — when the helper is long, recursive, or needs a name other code can call. A closure also cannot be recursive in the direct way a function can, and its type cannot be written out, which matters when the helper has to be stored or passed somewhere a named function would go.
A lifetime parameter: the diagnostic changes its wording
The same fault with a lifetime parameter produces the same code and a noticeably shorter diagnostic. There is no independence note, no second label naming the inner item, and the help is phrased differently:
fn outer<'a>(x: &'a str) {
fn inner(y: &'a str) -> usize {
y.len()
}
println!("{}", inner(x));
}
fn main() {
outer("hi");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:2:18
|
1 | fn outer<'a>(x: &'a str) {
| -- lifetime parameter from outer item
2 | fn inner(y: &'a str) -> usize {
| ^^ use of generic parameter from outer item
|
help: consider introducing lifetime `'a` here
|
2 | fn inner<'a>(y: &'a str) -> usize {
| ++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
The first span's label is lifetime parameter from outer item, and the help reads consider introducing lifetime 'a here, against the type case's try introducing a local generic parameter here. The repair the help offers is the same shape as before — declare the parameter on the inner item — and it works:
fn outer<'a>(x: &'a str) {
fn inner<'a>(y: &'a str) -> usize {
y.len()
}
println!("{}", inner(x));
}
fn main() {
outer("hi");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
2
The program prints 2 and exits with status 0. As with the type case, the inner 'a is a fresh lifetime parameter with a familiar name, not the outer lifetime reaching in. At the call, the compiler ties the inner lifetime to the reference actually passed, which is what the code needs. One practical consequence of the shorter diagnostic: if you search a build log for the independence note to find these errors, lifetime cases will not match it. Search on the code, E0401, or on the header text, which is identical across all three kinds of parameter.
A const generic parameter: the compiler's suggestion does not fix it
A const generic parameter fails the same way, in the body of the inner function just as in its signature. This program uses the outer function's N as the inner function's return value:
fn outer<const N: usize>(arr: [i32; N]) {
fn inner() -> usize {
N
}
println!("{}", inner());
println!("{}", arr.len());
}
fn main() {
outer::<3>([1, 2, 3]);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:3:9
|
1 | fn outer<const N: usize>(arr: [i32; N]) {
| - const parameter from outer item
2 | fn inner() -> usize {
| ----- generic parameter used in this inner function
3 | N
| ^ use of generic parameter from outer item
|
= 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 inner<N>() -> usize {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
The labels track the kind again: const parameter from outer item on the declaration span. The same error appears, word for word in structure, when N is used in the inner function's signature instead — as an array length in a parameter type, fn inner(a: [i32; N]). Now read the help block with care, because this is the one place in this guide where the compiler's suggestion is wrong for the job. It proposes fn inner<N> — angle brackets holding a bare N, which declares a type parameter named N, not a const parameter. Applying that suggestion verbatim does not repair the program:
fn outer<const N: usize>(arr: [i32; N]) {
fn inner<N>() -> usize {
N
}
println!("{}", inner::<3>());
println!("{}", arr.len());
}
fn main() {
outer::<3>([1, 2, 3]);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:3:9
|
1 | fn outer<const N: usize>(arr: [i32; N]) {
| - const parameter from outer item
2 | fn inner<N>() -> usize {
| ----- generic parameter used in this inner function
3 | N
| ^ use of generic parameter from outer item
|
= 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 inner<N, N>() -> usize {
| ++
error[E0747]: constant provided when a type was expected
--> main.rs:5:28
|
5 | println!("{}", inner::<3>());
| ^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0401, E0747.
For more information about an error, try `rustc --explain E0401`.
The body still names the outer const parameter, so E0401 remains — the suggestion has now escalated to the nonsense fn inner<N, N> — and the call site adds E0747, constant provided when a type was expected, because inner::<3> now tries to fill a type parameter with a constant. The working declaration spells the const parameter out in full, const N: usize, and the call passes the outer function's N through as the inner function's const argument:
fn outer<const N: usize>(arr: [i32; N]) {
fn inner<const N: usize>() -> usize {
N
}
println!("{}", inner::<N>());
println!("{}", arr.len());
}
fn main() {
outer::<3>([1, 2, 3]);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
3
3
The program prints 3 twice and exits with status 0: the inner function reports the const value it was given, and the array's length confirms it is the same 3. The lesson generalizes past this one suggestion. The help for E0401 is generated from the shape of the parameter list, and for type and lifetime parameters the generated form happens to be complete. For a const parameter it is not — the const keyword and the type are yours to add.
Bounds do not travel with the parameter
Declaring the inner parameter is only half of copying it. The outer parameter's bounds stay behind, and an inner item that relies on one fails — under a different code, which can send you looking in the wrong place. Here the outer parameter is bounded by Clone, the inner function declares a bare T, and its body calls clone:
fn outer<T: Clone>(x: T) {
fn inner<T>(y: T) -> T {
y.clone()
}
let _ = inner(x);
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0599]: no method named `clone` found for type parameter `T` in the current scope
--> main.rs:3:11
|
2 | fn inner<T>(y: T) -> T {
| - method `clone` not found for this type parameter
3 | y.clone()
| ^^^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `clone`, perhaps you need to restrict type parameter `T` with it:
|
2 | fn inner<T: Clone>(y: T) -> T {
| +++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.
There is no E0401 anywhere in this output, because the inner declaration is legitimate — the fault has moved one step later, to what the inner body assumes about its own parameter. The code is E0599, no method named clone, and its help points at the missing bound. Copying the bound across completes the repair:
fn outer<T: Clone>(x: T) -> T {
fn inner<T: Clone>(y: T) -> T {
y.clone()
}
inner(x)
}
fn main() {
println!("{}", outer(42));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. The rule to carry away: when you copy a parameter to an inner item, copy its whole declaration — name, kind, and bounds. A where clause on the outer function needs the same treatment. Whatever the outer body was allowed to assume about T, the inner body may assume only what the inner declaration repeats.
Inside an impl: the outer item is the impl block
Methods are not a shelter from this rule. A function nested inside a method cannot use the impl's type parameter, and the diagnostic's first span points at the impl line, not at the method — the outer item here is the impl block itself:
struct Foo<T>(T);
impl<T> Foo<T> {
fn show(&self, x: T) {
fn inner(y: T) {
let _ = y;
}
inner(x);
}
}
fn main() {
let f = Foo(1);
f.show(2);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:5:21
|
3 | impl<T> Foo<T> {
| - type parameter from outer item
4 | fn show(&self, x: T) {
5 | fn inner(y: T) {
| ----- ^ 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
|
5 | fn inner<T>(y: T) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
The same holds one level down, for a parameter declared on the method itself rather than on the impl. A generic method's inner function cannot use the method's T either; the first span then points at the method's parameter list, and everything else in the output is unchanged. And it holds inside a trait's default method body as well, where the inner function cannot use the default method's type parameter.
Inside an impl, though, there is a repair the plain function case does not have. Methods of an impl do share the impl's parameters — that is what being a method means — so turning the nested function into a private helper method removes the fault instead of working around it:
struct Foo<T>(T);
impl<T> Foo<T> {
pub fn foo(&self, x: T) -> T {
self.bar(x)
}
fn bar(&self, y: T) -> T {
y
}
}
fn main() {
let f = Foo(0);
println!("{} {}", f.0, f.foo(42));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
0 42
The program prints 0 42 and exits with status 0. The helper bar uses the impl's T in its signature with no declaration of its own, because as a method it is part of the impl item rather than an item nested inside one. This is usually the right shape for impl code: the helper stays private to the impl, callable from every method, and no parameter copying is needed. The exception is a trait default method, which has no single impl to move the helper into — there, the closure and copy-the-parameters repairs are the ones that still work.
Other inner items: type aliases, structs, and enums
Functions are not the only items that can be nested, and E0401 is not only about functions. A type alias written inside a generic function cannot use the outer parameter either:
fn outer<T>(x: T) {
type MaybeT = Option<T>;
let m: MaybeT = Some(x);
println!("{:?}", m.is_some());
}
fn main() {
outer(42);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:2:26
|
1 | fn outer<T>(x: T) {
| - type parameter from outer item
2 | type MaybeT = Option<T>;
| ------ ^ use of generic parameter from outer item
| |
| generic parameter used in this inner type alias
|
= 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 | type MaybeT<T> = Option<T>;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
The second label names the item kind — generic parameter used in this inner type alias — and the repair is the same declaration move, after which the alias is used with its parameter supplied:
fn outer<T>(x: T) {
type MaybeT<T> = Option<T>;
let m: MaybeT<T> = Some(x);
println!("{}", m.is_some());
}
fn main() {
outer(42);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
true
The program prints true and exits with status 0. A struct declared inside a generic function is in the same position. Its field cannot be typed with the outer parameter until the struct declares its own:
fn outer<T>(x: T) {
struct Wrap {
x: T,
}
let w = Wrap { x };
let _ = w.x;
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0401]: can't use generic parameters from outer item
--> main.rs:3:12
|
1 | fn outer<T>(x: T) {
| - type parameter from outer item
2 | struct Wrap {
| ---- generic parameter used in this inner struct
3 | x: T,
| ^ use of generic parameter from outer item
|
= 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 | struct Wrap<T> {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0401`.
With the parameter declared on the struct, the original body compiles unchanged:
fn outer<T>(x: T) {
struct Wrap<T> {
x: T,
}
let w = Wrap { x };
let _ = w.x;
}
fn main() {
outer(42);
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
done
The program prints done and exits with status 0. An enum declared inside a generic function behaves identically again: a variant field typed with the outer T fails with E0401, the second label reads generic parameter used in this inner enum, and the help offers enum Maybe<T>. The pattern across all four item kinds is one pattern. Whatever can be declared with angle brackets at the top level can be declared inside a function, and in both places it may use only the parameters in its own brackets.
What an inner function can still use
Independence has edges, and two of them are worth knowing before you restructure code around this error. First, items are visible to inner functions even though parameters are not. A constant declared in the outer function is an item, and the inner function can read it with no declaration and no error:
fn outer<T>(x: T) {
const KIND: &str = "value";
fn inner() -> &'static str {
KIND
}
println!("{}", inner());
let _ = x;
}
fn main() {
outer(42);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
value
The program prints value and exits with status 0. The note's exception — privacy and name resolution — is what makes this work: the inner function resolves names in the enclosing scope, so items declared around it are in reach. Generic parameters are not names in that sense; they belong to the outer item's declaration, and declaration is exactly what does not carry inward.
Second, an inner function whose work does not actually need the generic type can simply take concrete arguments, and then the outer parameter never comes up. Here the outer function is generic, but the helper doubles an i32, and the program compiles as written:
fn outer<T>(x: T) {
fn inner(y: i32) -> i32 {
y * 2
}
println!("{}", inner(21));
let _ = x;
}
fn main() {
outer("hi");
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. Passing values as arguments is the complete repair whenever the helper's job can be stated in concrete types — or in the helper's own generic types, which is the first fix again. What passing arguments cannot do is smuggle the outer parameter itself across: an argument's type has to be written in the inner signature, and writing the outer parameter's name there is the original error.
Neighboring error: naming an outer local is E0434
The mistake one step over from this one is naming the outer function's value — a local variable or a value parameter — inside a nested function. That is a different fault with a different code, because values are captured, not inherited:
fn outer(x: i32) {
fn inner() -> i32 {
x
}
println!("{}", inner());
}
fn main() {
outer(42);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0434]: can't capture dynamic environment in a fn item
--> main.rs:3:9
|
3 | x
| ^
|
= help: use the `|| { ... }` closure form instead
warning: unused variable: `x`
--> main.rs:1:10
|
1 | fn outer(x: i32) {
| ^ help: if this is intentional, prefix it with an underscore: `_x`
|
= note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0434`.
The code is E0434, can't capture dynamic environment in a fn item, and its help offers the closure form directly, with no parameter-declaration suggestion — because no declaration fixes a capture; the value has to be passed in as an argument or the function has to become a closure. The two faults can also arrive together. A nested function that uses both an outer generic parameter and an outer local is charged with both codes in one compile, E0401 for the parameter and E0434 for the value, each with its own spans and its own repair. When you see the pair, fix them separately: declare the parameter, and pass or capture the value. Neither repair substitutes for the other.
Neighboring errors: a struct as a bound is E0404, an unknown trait is E0405
Two more codes sit numerically beside E0401 and turn up in the same signatures, while meaning something unrelated to nesting. Writing a struct's name where a trait bound belongs fails with E0404:
struct Point {
x: i32,
}
fn outer() {
fn inner<T: Point>(y: T) {
let _ = y;
}
inner(42);
}
fn main() {
outer();
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0404]: expected trait, found struct `Point`
--> main.rs:6:17
|
6 | fn inner<T: Point>(y: T) {
| ^^^^^ not a trait
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0404`.
And a bound naming a trait that is not in scope fails with E0405:
fn outer() {
fn inner<T: Missing>(y: T) {
let _ = y;
}
inner(42);
}
fn main() {
outer();
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0405]: cannot find trait `Missing` in this scope
--> main.rs:2:17
|
2 | fn inner<T: Missing>(y: T) {
| ^^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0405`.
Neither program nests a generic parameter at all — the outer functions here are not even generic — and neither diagnostic mentions an outer item. The split to remember is by subject. E0401 is about whose parameter a name is. E0404 is about what kind of thing a bound names, a trait rather than a struct or another type. E0405 is about whether the named trait can be found at all, usually a missing import or a misspelled name. They share a neighborhood in the index and a habitat in generic signatures, and nothing else.
The exact rule: inner items do not inherit generic parameters
The official page for E0401 opens with the rule in one sentence: inner items do not inherit the generic parameters from the items they are embedded in. Its erroneous examples cover the three item kinds this guide's middle sections took up: a nested function using the outer function's T, a nested type alias for Option<T>, and a nested struct with a field of type T. Its summary of why is that 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. Its solutions are the ones verified above: if the item is a function, you may use a closure; for a generic item, you can copy over the parameters — and, it adds, be sure to copy over any bounds as well. For a function inside an impl, it offers the private helper method, and notes that for default impls in traits that solution will not work, while closures and copying the parameters still should. The page carries no note retiring or replacing this code; E0401 is the current code for this fault.
The Reference's page on generic parameters supplies the frame the rule sits in. Functions, type aliases, structs, enumerations, unions, traits, and implementations may be parameterized by types, constants, and lifetimes, and those parameters are listed in angle brackets, usually immediately after the name of the item. Parameters belong to the item they are listed on — which is the whole content of E0401, read from the other side. And the Reference's page on closure types explains why the closure repair is not an exception to the rule but a different mechanism: a closure expression produces a closure value whose type is approximately a struct containing the captured values. A closure can use the outer scope because it is a value built in that scope, holding what it captured. A nested function is an item, holds nothing, and declares everything it uses.
The fixes, ranked
- First, declare the parameter on the inner item, copying its full declaration — kind, name, and bounds. This is the repair the compiler offers, it keeps the helper nested and private to the outer function, and it works for functions, type aliases, structs, and enums alike. For a const parameter, write the declaration out in full,
const N: usize; the help block's bare<N>declares a type parameter and does not fix the error. - Second, inside an impl, make the nested function a private helper method. Methods share the impl's parameters, so no copying is needed and every method can call the helper. This does not apply inside a trait default method.
- Third, replace the nested function with a closure when the helper is short and local. The closure may use the outer parameters in its annotations and may capture the outer values outright — the only repair that also fixes an accompanying E0434.
- Fourth, move the inner item to the top level when it has outgrown the one caller. Once it declares its own parameters it is a standalone item, and the top level is its natural home.
- Fifth, restate the helper in concrete types and pass values as arguments, when the generic type was never actually needed. If the argument's type is the outer parameter, this collapses back into the first fix: the parameter has to be declared wherever its name is written.
Which fix for which cause
- Nested function uses the outer function's type parameter: E0401, labels type parameter from outer item and generic parameter used in this inner function, with the independence note. Declare the same parameter, with its bounds, on the inner function, or use a closure.
- Nested function uses the outer function's lifetime parameter: E0401, label lifetime parameter from outer item, help consider introducing lifetime here, and no independence note. Declare the same lifetime on the inner function.
- Nested function uses the outer function's const parameter, in its body or its signature: E0401, label const parameter from outer item. Declare
const N: usizein full on the inner function and pass the value as its const argument; do not apply the help block's bare<N>verbatim. - Inner function inside a generic impl or a generic method uses that parameter: E0401, first span on the impl line or the method's parameter list. Make it a helper method on the impl, declare the parameter on it, or use a closure.
- Inner function inside a trait default method uses the method's parameter: E0401. Declare the parameter or use a closure; there is no impl to hold a helper method.
- Nested type alias, struct, or enum uses the outer parameter: E0401, second label naming the inner item kind. Declare the parameter on that item and supply it at each use.
- Inner body relies on a bound the outer parameter had: no E0401; instead a missing-method or unsatisfied-bound error such as E0599 where the bound is used. Copy the bound onto the inner declaration as well.
- Nested function names an outer local or value parameter: E0434, not E0401. Pass the value as an argument or use a closure. Both codes together mean both faults are present; repair each.
- Bound names a struct or an unknown trait: E0404 or E0405, in nested and top-level signatures alike. Name a trait that exists and is in scope; nesting is not involved.
Checklist
- Read both spans before editing. The first names the outer item that owns the parameter — an outer function, an impl block, or a method — and the second names the inner item that used it. The repair is written on the inner item, never on the outer one.
- Copy the whole declaration, not just the name: the parameter's kind (type, lifetime, or const with its type) and every bound the inner body relies on. A missing bound surfaces later as a different error, typically E0599, at the use site.
- For a const parameter, distrust the generated suggestion. The help's angle brackets declare a type parameter; write
constand the type yourself, and pass the const value at the call. - Decide between item and closure on purpose. A nested function is an independent item: no captures, its own parameters, callable by name. A closure is a value: it captures, it uses the outer parameters freely, and it cannot be named as a type or called recursively.
- Inside an impl, reach for a helper method before copying parameters. It shares the impl's parameters by construction and stays available to every method. Inside a trait default method, skip it — closures and copied parameters are the repairs there.
- Check the code before searching for help. A generic parameter used inside a nested item is E0401; a captured local is E0434; a bound naming the wrong kind of thing or a missing trait is E0404 or E0405, and the four repairs are documented under four codes.
Related guides on this site
- E0434 Can't Capture Environment Fix
- E0463 Can't Find Crate Fix
- E0109 Generic Argument on Non-Generic Type Fix
- E0158 Constant Pattern Generic Parameter Fix
Sources
- Rust Compiler Error Index, E0401 — for inner items not inheriting the generic parameters of the items they are embedded in, for the erroneous examples of a nested function, type alias, and struct using an outer
T, for nested items being basically just like top-level items except that they can only be used from the item they are in, and for the solutions of using a closure, copying the parameters and their bounds to the inner item, and making a function inside an impl into a private helper method. - The Rust Reference, Generic parameters — for functions, type aliases, structs, enumerations, unions, traits, and implementations being parameterized by types, constants, and lifetimes, with the parameters listed in angle brackets immediately after the name of the item, or directly after
implfor implementations. - The Rust Reference, Closure types — for a closure expression producing a closure value with a unique anonymous type, and for a closure type being approximately equivalent to a struct which contains the captured values.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.