Skip to content

Compiler suggests giving types to typed variables #72130

Closed
@ISibboI

Description

@ISibboI
fn main() {
    let steps = 18;
    
    /*
        Uncomment none of the following lines: Compiler wants angle to have a type
        Uncomment any of the following lines: Compiler wants the first uncommented line to have a type
    */
    
    //let radius: f64 = 60.0;
    //let xoff: f64 = 5.0;
    //let yoff: f64 = 4.0;

    for i in 0..steps {
        let angle: f64 = 3.14f64 / steps.into() * i.into();
        println!("{}", angle);
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0284]: type annotations needed for `f64`
  --> src/main.rs:14:34
   |
9  |     let radius: f64 = 60.0;
   |         ------ consider giving `radius` a type
...
14 |         let angle: f64 = 3.14f64 / steps.into() * i.into();
   |                                  ^ cannot infer type for type `f64`
   |
   = note: cannot resolve `<f64 as std::ops::Div<_>>::Output == _`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

The compiler seems to fail to infer that I want to divide f64 by f64 here. I am no master of the type system so I just gotta accept that. The error message is confusing though, as it points me to give variables a type that are already typed. Rust version is 1.43.1 stable.

Also try uncommenting the commented lines, it will make the error message point to them, even though they are completely unrelated.

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