Skip to content

Type inference around Iterator::collect can result in confusing error messages #91423

Closed
@meithecatte

Description

@meithecatte

Given the following code:

fn process_slice(data: &[i32]) {
    todo!()
}

fn main() {
    let some_generated_vec = (0..10).collect();
    process_slice(&some_generated_vec);
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
 --> src/main.rs:6:9
  |
6 |     let some_generated_vec = (0..10).collect();
  |         ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[i32]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: a value of type `[i32]` cannot be built from an iterator over elements of type `{integer}`
    --> src/main.rs:6:38
     |
6    |     let some_generated_vec = (0..10).collect();
     |                                      ^^^^^^^ value of type `[i32]` cannot be built from `std::iter::Iterator<Item={integer}>`
     |
     = help: the trait `FromIterator<{integer}>` is not implemented for `[i32]`
note: required by a bound in `collect`

I am not sure what the ideal output would be here, but currently it suggests that collect wants to build an array of a specific size, which hasn't been specified.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions