How do I get an absolute value in Rust?
fn main() {
let mut number: i32 = -8;
number = number.abs();
println!("{}", number);
}
Remember that you must specify the datatype explicitly.
The answer mentioning std::num::abs
doesn't work anymore.
Instead, use:
i32::abs(n)
Nowadays, abs
is a method on most number types.
let value = -42i32;
let x = value.abs();