Skip to content

The compiler could have been more precise about missing type annotation.  #36495

Closed
@kindlychung

Description

@kindlychung

Here is the code:

#[derive(Debug)]
enum Message {
    Quit,
    ChangeColor(i32, i32, i32),
    Move { x: i32, y: i32 },
    Write(String),
}
enum BoardGameTurn {
    Move { squares: i32 },
    Pass,
}
fn main() {
    let x: Message = Message::Move { x: 3, y: 4 };
//  let v = "hello world".split_whitespace();
//  let v: Vec<String> = "hello world".split_whitespace().map(|x| x.to_string()).collect();
    let v = "hello world".split_whitespace().map(|x| x.to_string()).collect();
//    let v = vec!["Hello".to_string(), "World".to_string()];
    fn write_str(s: &str) -> Message {
        Message::Write(s.to_string())
    }
    let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
    let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
    for i in v1 {
        println!("{:?}", i);
    }
}

Compiler says:

cargo run
   Compiling rustcallc v0.1.0 (file:///Users/kaiyin/Documents/workspace-eclipse-neon/rustcallc)
error: the type of this value must be known in this context
  --> src/main.rs:21:28
   |
21 |     let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
   |                            ^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `rustcallc`.

The actual error is in this line:

    let v = "hello world".split_whitespace().map(|x| x.to_string()).collect();

which should have specified the type of v.

The compiler could have been more precise about the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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