abs()
to unsigned integer type RS-W1131Use of the abs()
method cast to an unsigned integer type is unidiomatic
and can cause a panic if called on the MIN
value. The unsigned_abs()
method
avoids panic or wrong behaviour when called on the MIN
value.
fn main() {
let x: i32 = -42;
let y = x.abs() as u32; // bad
}
fn main() {
let x: i32 = -42;
let y = x.unsigned_abs(); // fine
}