Closed
Description
If someone tries to write a hexadecimal float literal:
fn main() {
let _f:f32 = 0xAAf32;
}
They will get the following output:
Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:2:18
|
2 | let _f:f32 = 0xAAf32;
| --- ^^^^^^^ expected `f32`, found integer
| |
| expected due to this
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
Executing rustc --explain E0308
will give you:
Expected type did not match the received type.
Erroneous code examples:
fn plus_one(x: i32) -> i32 {
x + 1
}
plus_one("Not a number");
// ^^^^^^^^^^^^^^ expected `i32`, found `&str`
if "Not a bool" {
// ^^^^^^^^^^^^ expected `bool`, found `&str`
}
let x: f32 = "Not a float";
// --- ^^^^^^^^^^^^^ expected `f32`, found `&str`
// |
// expected due to this
This error occurs when an expression was used in a place where the compiler
expected an expression of a different type. It can occur in several cases, the
most common being when calling a function and passing an argument which has a
different type than the matching type in the function declaration.
This is different from the much simpler "binary float literal is not supported" message one gets, when trying to use a literal like 0b11f32
.
The problem is finding a "f32" at the end of a hexadecimal literal will be interpreted as part of the number, not a reference to float. If the user misses that, they will not get any help from either of the messages above.
Possible solution: the output of rustc --explain E0308
could include the following explanation at the end:
Also, Rust does not support hexadecimal float literals and cannot differentiate
them from hexadecimal integer literals; for example, `0xAAf32` is understood as
`700210 as i32`. Float literals should be written as `170_f32` or `0xAA as f32`.