Fix Rust E0282 "type annotations needed" — a Vec::new Never Filled, a collect() Followed by len(), a Bare None, and a Generic Function Returning Default
Nothing in these programs is the wrong type. One type is simply never stated anywhere, and inference has no evidence to choose it from. This guide separates four shapes that produce E0282 — an empty Vec that is only measured, a collect whose result is only measured, a bare None, and a generic function returning a Default value whose result is only measured — and matches each one to stating the single missing type where the diagnostic points.
Written November 22, 2026 · Tested with Rust 1.99.0 · 18 min read
- The rule underneath: inference needs evidence that leaves one unique type
- How to read the diagnostic
- The first failure: a Vec created with Vec::new and only ever measured
- The second failure: a collect whose result is only ever measured
Written November 22, 2026
Tested with Rust 1.99.0
What this guide is
Error E0282 is a missing type, not a wrong one. The error codes index states the cause in one line: the compiler could not infer a type and asked for a type annotation. Its fuller statement is that type inference did not result in one unique possible type, and extra information is required — in most cases, by the index's wording, provided by adding a type annotation, and sometimes by specifying a generic type parameter manually.
The four forms in this guide each reproduce E0282 with a small, complete program. A Vec created with Vec::new and only ever measured. An iterator collect whose result is only ever measured. A bare None, with no element type stated anywhere. And a call to a generic function that returns a Default value, whose result is only ever measured. In each family the failing program is small, the full compiler output is shown, and the fix states the one missing type at the place the diagnostic names — the binding — rather than annotating types the compiler already knows.
Two neighboring shapes are not in the families, because on this compiler they report different codes. A plain "42".parse() with no target type reports E0284, and a plain collect() whose result is printed with {:?} and never otherwise constrained reports E0283; both carry the same "type annotations needed" header wording, and the traps section shows the parse shape. E0282 is also not a type mismatch: a mismatch — a value of one known type used where another known type is required — is E0308, a different diagnostic for a different defect. Here, no type is wrong; one type is absent.
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 rule underneath: inference needs evidence that leaves one unique type
Rust is statically typed: it must know the types of all variables at compile time. The Rust Book chapter on data types states the other half of the rule in the same breath: the compiler can usually infer what type we want to use based on the value and how we use it. E0282 is what remains when that usual inference runs out of evidence — the value and its uses, taken together, still leave more than one possible type, or none in particular.
The error index works the Vec case as its example. Type Vec has a type parameter T; when calling Vec::new, barring any other later usage of the variable that allows the compiler to infer what type T is, the compiler needs to be told what it is. The Rust Book chapter on vectors makes the same point from the collection side: it adds a type annotation to its empty-vector example because, not inserting any values into the vector, Rust does not know what kind of elements we intend to store — and it notes that once values are inserted, Rust can often infer the type of value you want to store, so the annotation is not needed. Evidence, in other words, can come from later use as well as from an annotation; E0282 means neither source supplied it.
Generic producers are where the evidence runs out, because the missing type is a parameter of the producer rather than a property of any value already in hand. parse on a string slice is generic over its target: the standard library documentation for str::parse says it parses this string slice into another type, that it can parse any type that implements the FromStr trait, and that because parse is so general it can cause problems with type inference. collect is generic over its result in the same way: the standard library documentation for Iterator::collect says it transforms an iterator into a collection — it takes ownership of an iterator and produces whichever collection type you request — and that because collect() is so general, it can cause problems with type inference — naming the turbofish as the syntax that helps the inference algorithm understand specifically which collection you are trying to collect into. The Rust Book chapter on iterators shows collect consuming an iterator and collecting the resultant values into a collection data type, with the destination type stated on the binding in its example.
So the fix is always the same shape, even though the producer changes: state the missing type once, in any of the places the compiler accepts it — as an annotation on the binding, as a type argument in the path of the expression, or as a generic argument on the call — and inference does the rest. The error index adds that, in cases with more complex types, it is not necessary to annotate the full type: once the ambiguity is resolved, the compiler can infer the rest. A placeholder _ where the unknown type belongs does not count as stating it, as the traps section shows.
How to read the diagnostic
The header names what is missing. "type annotations needed for Vec<_>" and "type annotations needed for Option<_>" print the partly known type with the unknown part shown as _: the outer type was stated or implied by the expression, and the parameter inside it is the missing piece. A bare "type annotations needed", with no type after it, appears in this guide where the whole result type is open and the compiler only discovers the gap when a later line needs it.
Read the spans next. The caret span marks the binding whose type is incomplete, and a second span marks either the expression that left the type open — labeled "type must be known at this point" — or, in the collect and generic-function families, the later use that first required the type to be known, on the following line. The help line then offers the annotation with a placeholder where your type goes: "consider giving v an explicit type", in one family specifying where the type for type parameter T is specified. The placeholder in the help — T, /* Type */ — is not code to paste; it marks the spot your concrete type replaces. Follow the help literally, with a real type in that spot, and the diagnostic is answered in full.
The first failure: a Vec created with Vec::new and only ever measured
Vec::new creates an empty vector of some element type, and nothing in this program ever names or implies that type. len and is_empty work for a vector of any element type, so measuring the vector supplies no evidence about what it holds:
fn main() {
let v = Vec::new();
println!("{} {}", v.len(), v.is_empty());
}
$ rustc --edition 2021 fail_vec.rs -o fail_vec
error[E0282]: type annotations needed for `Vec<_>`
--> fail_vec.rs:2:9
|
2 | let v = Vec::new();
| ^ ---------- type must be known at this point
|
help: consider giving `v` an explicit type, where the type for type parameter `T` is specified
|
2 | let v: Vec<T> = Vec::new();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
The header prints the partly known type — Vec<_> — and the second span sits under Vec::new() with "type must be known at this point". The help rewrites the declaration with the element type stated. Follow it with a concrete type:
fn main() {
let v: Vec<i32> = Vec::new();
println!("{} {}", v.len(), v.is_empty());
}
$ rustc --edition 2021 fix_vec.rs -o fix_vec
$ ./fix_vec
0 true
The output is the measurement the failing program was trying to print: length 0, and empty. The annotation is the whole fix because the element type was the whole problem — the program never needed the vector to hold anything for the compiler to need to know what it would hold. The error index gives the same repair in the path instead of on the binding, Vec::<i32>::new(), for cases where there is no convenient binding to annotate.
The second failure: a collect whose result is only ever measured
Here the iterator's element type is known — the range yields integers — and the open type is the collection collect is to build. Many collection types can be built from an iterator, and the only later use, len, is one that collections generally provide, so it does not choose among them. Note where the second span lands: not on collect, but on the len call on the next line, the first place the type had to be known:
fn main() {
let v = (1..=3).collect();
println!("{}", v.len());
}
$ rustc --edition 2021 fail_collect.rs -o fail_collect
error[E0282]: type annotations needed
--> fail_collect.rs:2:9
|
2 | let v = (1..=3).collect();
| ^
3 | println!("{}", v.len());
| - type must be known at this point
|
help: consider giving `v` an explicit type
|
2 | let v: Vec<_> = (1..=3).collect();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
The header here is the bare wording, with no type after it, and the help offers the binding annotation with the element left as _ — enough, because the element was never the unknown part. Stating the collection type answers it:
fn main() {
let v: Vec<i32> = (1..=3).collect();
println!("{}", v.len());
}
$ rustc --edition 2021 fix_collect.rs -o fix_collect
$ ./fix_collect
3
The output is the count the failing program was trying to print: the three values from the range, collected. The standard library's collect documentation gives the same repair in the other position, the turbofish on the call — collect::<Vec<i32>>() in its example — for chains where annotating the binding is awkward. Either position states the same single fact: which collection type collect is building.
The third failure: a bare None
None is a variant of Option<T>, and a None carries no value from which T could be inferred. Printing it with {:?} does not help: Option<T> can be printed that way for many choices of T, so the print supplies no evidence about the element type:
fn main() {
let x = None;
println!("{:?}", x);
}
$ rustc --edition 2021 fail_none.rs -o fail_none
error[E0282]: type annotations needed for `Option<_>`
--> fail_none.rs:2:9
|
2 | let x = None;
| ^ ---- type must be known at this point
|
help: consider giving `x` an explicit type, where the type for type parameter `T` is specified
|
2 | let x: Option<T> = None;
| +++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
The header again prints the partly known type — Option<_> — and the span under None says the type must be known at this point. The missing fact is the element type, and the binding is where the help points:
fn main() {
let x: Option<i32> = None;
println!("{:?}", x);
}
$ rustc --edition 2021 fix_none.rs -o fix_none
$ ./fix_none
None
The output is the value the failing program was trying to print: None, now as an Option<i32>. The shape generalizes beyond this example: any producer whose result is a generic container with no payload in hand — an empty collection, a None, an Ok whose error type nothing pins — leaves its parameter to be stated, because there is no value anywhere in the program that carries the missing type.
The fourth failure: a generic function returning Default, its result only ever measured
A function can be the producer. make returns whatever type its caller chooses, as long as that type implements Default, and the call in main chooses nothing: the result is only measured with len, which many types provide, so the return type stays open. As in the collect family, the second span lands on the later use — the len call — rather than on the call itself:
fn make<T: Default>() -> T {
T::default()
}
fn main() {
let v = make();
println!("{}", v.len());
}
$ rustc --edition 2021 fail_make.rs -o fail_make
error[E0282]: type annotations needed
--> fail_make.rs:6:9
|
6 | let v = make();
| ^
7 | println!("{}", v.len());
| - type must be known at this point
|
help: consider giving `v` an explicit type
|
6 | let v: /* Type */ = make();
| ++++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
The help is the plainest in this guide — "consider giving v an explicit type", with /* Type */ marking the spot — because the compiler has no partly known type to print: the whole return type is the unknown. State it on the binding, choosing a type that satisfies the function's bound:
fn make<T: Default>() -> T {
T::default()
}
fn main() {
let v: Vec<i32> = make();
println!("{}", v.len());
}
$ rustc --edition 2021 fix_make.rs -o fix_make
$ ./fix_make
0
The output is the measurement the failing program was trying to print: the default Vec<i32> is empty, so its length is 0. The annotation did two jobs at once here: it told make which type to produce, and it had to name a type that actually implements Default and provides the later len use — an annotation that names a type failing either condition is a different error, not a fix for this one.
The fixes in reach order
- Annotate the binding when there is one:
let v: Vec<i32> = Vec::new();,let x: Option<i32> = None;— the position every help line in this guide points at first. - State the type in the path of the expression when the binding is not the natural place:
Vec::<i32>::new(), the error index's alternative for theVeccase. - Use the turbofish on a generic call when the producer sits inside a chain:
collect::<Vec<i32>>()for an iterator chain,parse::<u32>()in the standard library'sparseexample. - Let a later use supply the type when the program genuinely goes on to use the value in a type-specific way — pushing a value of a known type into the vector, for example — and no annotation is then needed at all; the traps section shows that shape compiling.
- State only the part that is missing: an annotation may leave the parts inference already knows as
_, as in thecollecthelp'sVec<_>, but the unknown parameter itself must be replaced with a concrete type somewhere.
Traps
- Writing the annotation with a placeholder where the missing type belongs does not work.
Vec<_>looks like an annotation, but the_is exactly the part the compiler is asking for, and the same program fails again with E0282 — now pointing inside the call, at the type parameter declared onVec:
fn main() {
let v: Vec<_> = Vec::new();
println!("{}", v.len());
}
$ rustc --edition 2021 probe_placeholder_vec.rs -o probe_placeholder_vec
error[E0282]: type annotations needed
--> probe_placeholder_vec.rs:2:21
|
2 | let v: Vec<_> = Vec::new();
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Vec`
|
help: consider specifying a concrete type for the type parameter `T`
|
2 | let v: Vec<_> = Vec::</* Type */>::new();
| ++++++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0282`.
collect whose iterator already determines it, for instance. Where the placeholder stands in for the missing fact itself, it restates the question instead of answering it.
- The turbofish with a placeholder does not work either, and on this compiler the parse shape reports a neighboring code. "42".parse::<_>() names the syntax but not the type, and the failure is E0284, not E0282 — the same "type annotations needed" header, with a note that the FromStr error side cannot be satisfied:fn main() {
let n = "42".parse::<_>().unwrap();
println!("{}", n);
}
$ rustc --edition 2021 probe_parse_placeholder.rs -o probe_parse_placeholder
error[E0284]: type annotations needed
--> probe_parse_placeholder.rs:2:9
|
2 | let n = "42".parse::<_>().unwrap();
| ^ ----- type must be known at this point
|
= note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider giving `n` an explicit type
|
2 | let n: /* Type */ = "42".parse::<_>().unwrap();
| ++++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0284`.
That code difference is worth knowing when searching for help: an unannotated parse is the shape the data-types chapter of the Rust Book uses to introduce type annotations, and the remedy there and here is the same — a concrete target type, stated on the binding or in the turbofish — but the code printed by this compiler for the parse shape is E0284, while the four families in this guide print E0282.
- Adding a type-specific use is not a trap — it is the other way out, and it is worth recognizing so an annotation is not added where the program was about to supply the evidence itself. Pushing one integer into the vector pins the element type, and the program that failed as the first family compiles with no annotation at all:
fn main() {
let mut v = Vec::new();
v.push(1);
println!("{:?}", v);
}
$ rustc --edition 2021 probe_push_pins.rs -o probe_push_pins
$ ./probe_push_pins
[1]
The output is the vector holding the pushed value. This is the error index's condition in action: a later usage that allows the compiler to infer the type parameter removes the need to state it. It is a fix only when the pushed value is work the program actually had to do — an element pushed solely to satisfy the compiler, and never used, is an annotation written the long way round.
Decision summary
- The header prints a partly known type with a
_inside it (Vec<_>,Option<_>): state that parameter, on the binding or in the path. - The header is bare and the second span sits on a later line: the later use is where the type was first required — state the result type of the producer (
collect, the generic call) on the binding, or with the turbofish on the call. - The help shows
Tor/* Type */: those are placeholders for your concrete type, not code to paste. - An annotation was added and the same E0282 remains: check whether the annotation left the missing part as
_— the placeholder restated the question. - The code is E0284 or E0283 under the same header wording: a neighboring inference failure, as with an unannotated
parse(E0284) or acollectwhose result is only printed (E0283) — the remedy, one concrete type stated, is the same, but search and documentation lookups should use the code your compiler printed. - The report is E0308 instead: a type mismatch — two known types disagree — which no annotation of a missing type will repair.
Checklist
- The missing type identified from the header and spans before editing: a parameter inside a partly known type, or the whole result type of a generic producer.
- The concrete type stated exactly once, in a position the compiler accepts: binding annotation, path, or turbofish.
- No placeholder
_left standing where the missing type belongs. - Where the annotation names the result of a generic function, the named type checked against the function's bounds and against the later uses of the value.
- Later uses checked for evidence the program already had: a type-specific use may make the annotation unnecessary.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- Fix Rust E0499 "cannot borrow as mutable more than once at a time" — Two Bindings, a Function Call, Two Vec Elements, and a Method Call
- Fix Rust E0596 "cannot borrow as mutable" — a Vec Without mut, a Function Taking &Vec, a &self Method Pushing to a Field, and a Shared Reference in a Binding
- E0107 Wrong Generics Fix
- E0509 Cannot Move Out Fix
Sources
- Rust, Error codes index: E0282 — the definition of the error: the compiler could not infer a type and asked for a type annotation; type inference did not result in one unique possible type and extra information is required, provided in most cases by a type annotation and sometimes by specifying a generic parameter manually; the
Vec::newexample, with the type stated on the variable or in the path, and the note that once the ambiguity is resolved the compiler can infer the rest. - The Rust Programming Language, Data Types — Rust is statically typed and must know the types of all variables at compile time, the compiler can usually infer the type from the value and how it is used, and in cases when many types are possible, such as converting a string with
parse, a type annotation must be added. - The Rust Programming Language, Storing Lists of Values with Vectors — the empty-vector annotation: because no values are inserted, Rust does not know what kind of elements are intended to be stored; with initial values Rust will infer the type, so the annotation is rarely needed, and values pushed afterwards likewise let Rust infer the type, so no annotation is needed.
- The Rust Programming Language, Processing a Series of Items with Iterators —
collectconsumes the iterator and collects the resultant values into a collection data type, with the destination type stated on the binding in the chapter's example. - Rust Standard Library, Iterator::collect —
collecttransforms an iterator into a collection, producing whichever collection type you request; because it is so general it can cause problems with type inference, and the turbofish helps the inference algorithm understand specifically which collection you are trying to collect into. - Rust Standard Library, str::parse —
parseparses a string slice into another type and can parse any type that implements theFromStrtrait; because it is so general it can cause problems with type inference, and its examples state the target type on the binding or with the turbofish.
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.