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