I was trying to raise an integer to a power using the caret operator (^
), but I am getting surprising results, e.g.:
assert_eq!(2^10, 8);
Searches on DuckDuckGo and Google didn't reveal anything about it.
How can I perform exponentiation in Rust?
The caret operator
^
is not used for exponentiation, it's the bitwise XOR operator.Rust provides exponentiation via methods
pow
andchecked_pow
which guards against overflows.Thus, to raise 2 to the power of 10, do: