Search⌘ K

Negative Numbers are Annoying

Explore how Rust evaluates negative numbers and the absolute value method, uncovering why negative signs apply after method calls. Learn to fix common mistakes by using parentheses or variables to obtain the expected results.

We'll cover the following...

What’s the absolute value of -5? The answer is 5. So, Rust should say the same thing, right? Let’s find out!

Rust 1.40.0
fn main() {
println!("x == {}", -5_i32.abs());
}

If you’re expecting the output of this program to be x == 5, ...