Reputation: 1250
I could not understand this code. How comes -a
yields different result from -7
?
fn main() {
let a: i32 = 7; // or any other integer type
let b = 4;
assert_eq!((-a).rem_euclid(b), 1);
assert_eq!(-7_i32.rem_euclid(4), -3);
assert_eq!(-a, -7_i32);
}
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3147fbbf7aeeeff5510522f9af2d12f2
Upvotes: 0
Views: 97
Reputation: 917
It's because -7_i32.rem_euclid(4) == -(7_i32.rem_euclid(4))
. The unary negation has lower precedence than the method call.
Upvotes: 4