Skip to content

Error message provides suggested fix that does not compile #51972

Closed
@dralley

Description

@dralley

When I attempt to compile the code:

fn main() {
    let mut sum = 0i32;
    
    for i in 0..10 {
        sum += i.pow(2);
    }
    
    println!("{}", sum);
}

I get back an error message with a 'help' suggestion:

error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
 --> test.rs:5:18
  |
5 |         sum += i.pow(2);
  |                  ^^^
help: you must specify a type for this binding, like `i32`
  |
4 |     for i: i32 in 0..10 {
  |         ^^^^^^

error: aborting due to previous error

This is a great error message however the suggested code unfortunately does not work. Adding : i32 to the previous code sample as suggested will also fail with the error:

error: missing `in` in `for` loop
 --> test.rs:4:10
  |
4 |     for i: i32 in 0..10 {
  |          ^ help: try adding `in` here

error: expected expression, found `:`
 --> test.rs:4:10
  |
4 |     for i: i32 in 0..10 {
  |          ^ expected expression

error: aborting due to 2 previous errors

What does work, is changing 0..10 to 0i32..10. The rustc-suggested syntax does seem reasonable to me (as a newbie) so it's a little surprising that it's invalid.

rustc version:

rustc 1.27.0 (3eda71b00 2018-06-19)
binary: rustc
commit-hash: 3eda71b00ad48d7bf4eef4c443e7f611fd061418
commit-date: 2018-06-19
host: x86_64-unknown-linux-gnu
release: 1.27.0
LLVM version: 6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions