Rust
E0121 Placeholder Type Fix
You write _ where a type belongs on a function, a static, or a struct, expecting the compiler to fill it in the way it does inside a function body — and it refuses outright. E0121 is not an inference failure: the placeholder is banned on item signatures even when the type is obvious from the code right next to it. The diagnostic names the kind of item, and the repair is always one of two moves: write the concrete type the item actually has, or declare a generic parameter when the type is genuinely the caller's choice.
Written November 26, 2026 · Tested with Rust 1.99.1 · 15 min read
- The error: a placeholder parameter
- The error: a placeholder return type
- The error: statics, consts, fields, and aliases
- The error: a placeholder nested inside a signature type
Written November 26, 2026
Tested with Rust 1.99.1
What this guide is
The placeholder _ in a type position asks the compiler to infer the type from the surrounding information. Inside a function body that request is routine: let x: _ = 42; compiles, and Vec<_> on a binding is the standard way to state a collection's outer type while leaving its element type to inference. On an item signature the same request is forbidden outright. The Rust Reference states the constraint in one sentence: the inferred type cannot be used in item signatures. The error codes index gives the same rule from the reporting side: the type placeholder _ was used within a type on an item's signature.
E0121 is the report for that ban, and it is worth separating from the inference errors it resembles. It is not E0282 or E0283, the "type annotations needed" pair: those fire where the programmer wrote no placeholder at all and inference either found no type or found several. E0121 fires where the programmer did write _, in a place where inference is not permitted to answer — even when the answer is sitting in the next token. A function that returns the literal 5 still fails with -> _, and the compiler's own help line supplies the i32 it is not allowed to infer for you there. The signature of an item is a contract other code is compiled against, and the contract has to be written out.
This guide reproduces the error in every common signature position — function parameter, return type, static, const, struct field, type alias, a nested Vec<_> inside a parameter, a method return, and a trait method declaration — then gives the two repairs that cover all of them, and finishes with the places _ is legal, because most E0121 fixes start from code that worked in a body and was moved into a signature.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The error: a placeholder parameter
The smallest form is a placeholder in a function parameter. The call in main passes 42, so the intended type is not in doubt — and it makes no difference:
fn foo(x: _) {}
fn main() { foo(42); }
$ rustc --edition 2021 param.rs -o param.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> param.rs:1:11
|
1 | fn foo(x: _) {}
| ^ not allowed in type signatures
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
Read the header closely, because it carries the whole diagnosis: "the placeholder _ is not allowed within types on item signatures for functions". The last words name the kind of item — here, functions. The caret marks the placeholder itself, labeled "not allowed in type signatures". There is no note about what the type should be and no complaint about the call: the defect is the _ in that position, full stop.
The error: a placeholder return type
The error index's own first example is the return-type form:
fn foo() -> _ { 5 }
fn main() { println!("{}", foo()); }
$ rustc --edition 2021 ret.rs -o ret.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
--> ret.rs:1:13
|
1 | fn foo() -> _ { 5 }
| ^ not allowed in type signatures
|
help: replace with the correct return type
|
1 - fn foo() -> _ { 5 }
1 + fn foo() -> i32 { 5 }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
This is the clearest proof that E0121 is a ban, not an inference failure. The body returns 5; the compiler knows the type well enough to write the help line replacing _ with i32 — and still reports the error, because knowing the type does not license leaving the placeholder in the signature. The help is the fix, verbatim: replace the placeholder with the correct return type.
The same shape inside an implementation block reports identically, with the method's return position marked:
struct Counter { n: i32 }
impl Counter { fn get(&self) -> _ { self.n } }
fn main() { let c = Counter { n: 7 }; println!("{}", c.get()); }
$ rustc --edition 2021 impl_method.rs -o impl_method.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
--> impl_method.rs:2:33
|
2 | impl Counter { fn get(&self) -> _ { self.n } }
| ^ not allowed in type signatures
|
help: replace with the correct return type
|
2 - impl Counter { fn get(&self) -> _ { self.n } }
2 + impl Counter { fn get(&self) -> i32 { self.n } }
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
The error: statics, consts, fields, and aliases
Functions are not the only items with signatures. A static written with a placeholder is the error index's second example, and its help names the fully specified type:
static BAR: _ = "test";
fn main() { println!("{}", BAR); }
$ rustc --edition 2021 static_.rs -o static_.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
--> static_.rs:1:13
|
1 | static BAR: _ = "test";
| ^ not allowed in type signatures
|
help: replace this with a fully-specified type
|
1 - static BAR: _ = "test";
1 + static BAR: &str = "test";
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
A const behaves the same way, with the item kind named as constants:
const N: _ = 42;
fn main() { println!("{}", N); }
$ rustc --edition 2021 const_.rs -o const_.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
--> const_.rs:1:10
|
1 | const N: _ = 42;
| ^ not allowed in type signatures
|
help: replace this with a fully-specified type
|
1 - const N: _ = 42;
1 + const N: i32 = 42;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
A struct field is part of the struct's signature — the field types are the type's public contract — so a placeholder field fails too, this time without a help line:
struct S { x: _ }
fn main() { let s = S { x: 42 }; println!("{}", s.x); }
$ rustc --edition 2021 structfield.rs -o structfield.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
--> structfield.rs:1:15
|
1 | struct S { x: _ }
| ^ not allowed in type signatures
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
And a type alias cannot be defined as the placeholder either. An alias exists to give a type a name; defining it as _ would give the name no type at all:
type T = _;
fn main() { let x: T = 42; println!("{}", x); }
$ rustc --edition 2021 typealias.rs -o typealias.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for type aliases
--> typealias.rs:1:10
|
1 | type T = _;
| ^ not allowed in type signatures
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
The error: a placeholder nested inside a signature type
Nesting the placeholder inside a larger type does not move it out of the signature. Vec<_> on a let binding is legal and common; the identical spelling in a parameter is E0121, because the whole parameter type is the signature:
fn total(v: Vec<_>) -> i32 { v.iter().sum() }
fn main() { println!("{}", total(vec![1, 2, 3])); }
$ rustc --edition 2021 vec_param.rs -o vec_param.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> vec_param.rs:1:17
|
1 | fn total(v: Vec<_>) -> i32 { v.iter().sum() }
| ^ not allowed in type signatures
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
The caret sits on the inner _, not on the Vec — the outer type was written out and is fine; the placeholder inside it is the banned part. This is the form the error most often takes in real code, because Vec<_> reads as a single idiom carried over from binding annotations.
The error: a trait method declaration
A trait method declaration is a signature with no body at all, so there is nothing for inference to work from even in principle. It fails with the item kind named as methods:
trait Show { fn name(&self) -> _; }
fn main() {}
$ rustc --edition 2021 trait_fn.rs -o trait_fn.bin
error[E0121]: the placeholder `_` is not allowed within types on item signatures for methods
--> trait_fn.rs:1:32
|
1 | trait Show { fn name(&self) -> _; }
| ^ not allowed in type signatures
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0121`.
The fix: name the concrete type
For most occurrences the type is already decided by the code — the body returns one type, the initializer is one type, the callers pass one type — and the fix is to write it. The parameter program with the type named compiles and runs as intended:
fn foo(x: i32) { println!("{}", x); }
fn main() { foo(42); }
$ rustc --edition 2021 fixed_param.rs -o fixed_param.bin
$ ./fixed_param.bin
42
The return-type and static fixes are the ones the compiler's help lines proposed. The return type:
fn foo() -> i32 { 5 }
fn main() { println!("{}", foo()); }
$ rustc --edition 2021 fixed_ret.rs -o fixed_ret.bin
$ ./fixed_ret.bin
5
The static, with the string literal's type written as &str:
static BAR: &str = "test";
fn main() { println!("{}", BAR); }
$ rustc --edition 2021 fixed_static.rs -o fixed_static.bin
$ ./fixed_static.bin
test
The remaining shapes take the same move, one type each. The const:
const N: i32 = 42;
fn main() { println!("{}", N); }
$ rustc --edition 2021 fixed_const.rs -o fixed_const.bin
$ ./fixed_const.bin
42
The struct field:
struct S { x: i32 }
fn main() { let s = S { x: 42 }; println!("{}", s.x); }
$ rustc --edition 2021 fixed_struct.rs -o fixed_struct.bin
$ ./fixed_struct.bin
42
The alias, which now names the type it was always going to stand for:
type T = i32;
fn main() { let x: T = 42; println!("{}", x); }
$ rustc --edition 2021 fixed_alias.rs -o fixed_alias.bin
$ ./fixed_alias.bin
42
The nested case, where only the inner placeholder needed a type — the Vec spelling stays, its element type is written:
fn total(v: Vec<i32>) -> i32 { v.iter().sum() }
fn main() { println!("{}", total(vec![1, 2, 3])); }
$ rustc --edition 2021 fixed_vec.rs -o fixed_vec.bin
$ ./fixed_vec.bin
6
The method inside the implementation block:
struct Counter { n: i32 }
impl Counter { fn get(&self) -> i32 { self.n } }
fn main() { let c = Counter { n: 7 }; println!("{}", c.get()); }
$ rustc --edition 2021 fixed_impl.rs -o fixed_impl.bin
$ ./fixed_impl.bin
7
And the trait declaration, fixed in the trait and matched in the implementation:
trait Show { fn name(&self) -> &str; }
struct Cat;
impl Show for Cat { fn name(&self) -> &str { "cat" } }
fn main() { let c = Cat; println!("{}", c.name()); }
$ rustc --edition 2021 fixed_trait.rs -o fixed_trait.bin
$ ./fixed_trait.bin
cat
The fix: declare a generic parameter when the caller chooses
Sometimes the placeholder was reaching for something real: the function genuinely should work for whatever type the caller passes, and no single concrete type is the right thing to write. That is what generic parameters are for. Declaring T on the function and using T in the signature states the contract honestly — the type is a parameter, chosen at each call — instead of asking inference to invent it:
fn foo<T>(x: T) -> T { x }
fn main() { println!("{}", foo(42)); }
$ rustc --edition 2021 generic_param.rs -o generic_param.bin
$ ./generic_param.bin
42
The difference from the placeholder matters. _ in a signature says "infer this, once, from this item" — which is exactly what signatures may not ask. T says "this item is defined for every type; each call fixes it" — a statement about the item itself, written in the signature where it belongs. Choose this fix when different calls really do pass different types. When every call passes the same type, the concrete fix above is the honest one, and a generic parameter only adds a parameter nobody varies.
Where _ is allowed
The ban is about position, not about the placeholder itself. In three positions the same _ compiles and runs, and knowing them is what keeps the fix from overshooting into annotating code that was never broken.
A type ascription on a let binding inside a function body:
fn main() { let x: _ = 42; println!("{}", x); }
$ rustc --edition 2021 let_type.rs -o let_type.bin
$ ./let_type.bin
42
A partial generic argument on a binding — the Reference's own example shape — where the element type is left to inference while the collection type is stated:
fn main() { let v: Vec<_> = vec![1, 2, 3]; println!("{}", v.len()); }
$ rustc --edition 2021 let_vec.rs -o let_vec.bin
$ ./let_vec.bin
3
A generic argument in an expression, the error index's allowed example — the turbofish on collect names the collection and leaves its element type open:
fn main() { let parts = "a4a".split('4').collect::<Vec<_>>(); println!("{:?}", parts); }
$ rustc --edition 2021 collect_turbo.rs -o collect_turbo.bin
$ ./collect_turbo.bin
["a", "a"]
And a closure parameter. A closure is an expression, not an item, so it has no item signature for the ban to attach to — its parameter types are inferred from how it is called:
fn main() { let f = |x: _| x + 1; println!("{}", f(41)); }
$ rustc --edition 2021 closure.rs -o closure.bin
$ ./closure.bin
42
The pattern to hold onto: a body can ask for inference because everything inference needs is in the same function, checked in the same pass. A signature is read by other code, other modules, and other crates, and it has to stand on its own — so the placeholder that is a convenience in a body is an omission in a signature.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Placeholder as a function parameter type | E0121, item kind "for functions" | Write the concrete type callers pass, or declare a generic parameter if callers vary |
| Placeholder as a return type, free function or method | E0121, item kind "for return types", usually with a help line naming the type | Write the type the body returns — often exactly the type in the help line |
Placeholder on a static or const |
E0121, item kind "for static variables" or "for constants", with a help line | Write the fully specified type — &str for a string literal static, the integer type for a numeric const |
| Placeholder as a struct field type | E0121, item kind "for structs" | Write the field's concrete type |
| Placeholder as a type alias target | E0121, item kind "for type aliases" | Write the type the alias stands for |
Placeholder nested in a signature type, such as Vec<_> as a parameter |
E0121, caret on the inner _ |
Write the inner type (Vec<i32>); the outer spelling is fine as written |
| Placeholder in a trait method declaration | E0121, item kind "for methods" | Write the return type in the trait, and match it in every implementation |
Placeholder in a let binding, a turbofish, or a closure parameter |
No error | Leave it — those positions are not item signatures and inference is allowed there |
Checklist
- Read the end of the diagnostic header first. It names the kind of item — functions, return types, static variables, constants, structs, type aliases, methods — and that tells you which signature you are repairing.
- Do not debug inference. E0121 fires even when the type is obvious from the body or the initializer; adding annotations elsewhere, changing the body, or constraining callers will not move it. Only replacing the
_in the signature will. - Follow the help line when one is printed. For return types, statics, and consts the compiler usually proposes the fully specified type itself, and accepting it verbatim is the correct fix.
- Check for nesting. A
Vec<_>or other generic type carried over from a binding annotation keeps its_inside the signature, and the caret points at that inner placeholder. - Reach for a generic parameter only when callers genuinely vary. One concrete type at every call means the concrete fix; a generic written out of habit hides a fixed contract behind a parameter.
- Leave body positions alone. Placeholders in
letascriptions, in generic arguments on expressions such ascollect::<Vec<_>>(), and in closure parameters are legal and idiomatic — a sweep that replaces every_in the file will damage working code.
Related guides on this site
- E0091 Unused Type Parameter Fix
- E0118 Inherent Impl on Non-Nominal Type Fix
- E0109 Generic Argument on Non-Generic Type Fix
- E0184 Copy on Type With Drop Fix
Sources
- Rust Compiler Error Index, E0121 — for the type placeholder
_being used within a type on an item's signature, for thefn foo() -> _andstatic BAR: _erroneous examples and their explicit-type fixes, and forcollect::<Vec<_>>()as the use of the placeholder outside an item's signature. - The Rust Reference, Inferred type — for
_as the inferred type that asks the compiler to infer from surrounding information, for its common use in generic arguments such aslet x: Vec<_>, and for the rule that the inferred type cannot be used in item signatures. - The Rust Programming Language, Generic Data Types — for declaring a generic type parameter on a function and using it in the function's signature, the repair for a signature whose type is genuinely the caller's choice.
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.