Skip to content

Type inference failure on From::from tries to blame Iterator::collect #78055

Closed
@saethlin

Description

@saethlin

This code:

use rand::Rng;
use std::net::Ipv4Addr;

fn main() {
    let rng = rand::thread_rng();
    let ips: Vec<_> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
}

Produces:

error[E0283]: type annotations needed for `Vec<Ipv4Addr>`
 --> src/main.rs:6:44
  |
6 |     let ips: Vec<_> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
  |         ---                                ^^^^^^^^^^^^^^ cannot infer type for struct `Ipv4Addr`
  |         |
  |         consider giving `ips` the explicit type `Vec<Ipv4Addr>`, where the type parameter `Ipv4Addr` is specified
  |
  = note: cannot satisfy `Ipv4Addr: From<_>`
  = note: required by `from`

At least following the suggestion doesn't result in the error message going any farther off the rails, but it does look quite silly:

error[E0283]: type annotations needed for `Vec<Ipv4Addr>`
 --> src/main.rs:6:51
  |
6 |     let ips: Vec<Ipv4Addr> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
  |         ---                                       ^^^^^^^^^^^^^^ cannot infer type for struct `Ipv4Addr`
  |         |
  |         consider giving `ips` the explicit type `Vec<Ipv4Addr>`, where the type parameter `Ipv4Addr` is specified
  |
  = note: cannot satisfy `Ipv4Addr: From<_>`
  = note: required by `from`

Diagnostics pasted from rustc 1.49.0-nightly (e3051d8c2 2020-10-16), but same behavior on stable 1.47.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-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