Consider the code below:
pub fn main() {
let data = [
[-200, 100],
[-300, 110],
[50, 120],
[-400, 150],
[600, 80],
];
let s = data[0][0];
let q = data[1][0];
println!("{}", s.abs().max(q.abs()));
}
It fails with:
error[E0599]: no method named `abs` found for type `{integer}` in the current scope
--> src/main.rs:12:20
|
12 | println!("{}", s.abs().max(q.abs()));
| ^^^
error[E0599]: no method named `abs` found for type `{integer}` in the current scope
--> src/main.rs:12:32
|
12 | println!("{}", s.abs().max(q.abs()));
| ^^^
error: aborting due to 2 previous errors
But when I add explicit types when declaring data
, it works!
pub fn main() {
let data: [[i32; 2]; 5] = [ // Type is explicitly specified
[-200, 100],
[-300, 110],
[50, 120],
[-400, 150],
[600, 80],
];
let s = data[0][0];
let q = data[1][0];
println!("{}", s.abs().max(q.abs()));
}
My initial thinking is this behavior is on account of some limitations of the type-inference system. If that indeed is the case, may be we could provide little more context in the error, say, "unable to infer granular type of integer"?
On the other hand, if this could be addressed that will be great as the behavior is somewhat unintuitive.
$ rustc -V
rustc 1.36.0 (a53f9df32 2019-07-03)