How to raise a number to a power in Rust? (exponentiation operator)
Hello, colleagues! I just started learning Rust and I have a seemingly simple problem. In Python, I'm used to writing `2 ** 10` to get 1024. But in Rust, this code doesn't work.
Here's an example of my code:
```rust
fn main() {
let base = 2;
let exp = 10;
let result = base ** exp; // Error!
println!("{} to the power of {} = {}", base, exp, result);
}
The compiler gives an error: `error: expected expression, found **`. Then I tried `^` hoping it would be an XOR or power operator (like in some languages), but that also didn't work as expected.
I understand that Rust doesn't have a built-in operator for exponentiation, but how do I do it correctly? Do I need to include some library? I need to raise both integers (i32, u64) and floating-point numbers to a power.
No answers yet.
Log in to leave an answer