In Java, I could do this.
int diff = 'Z' - 'A'; // 25
I have tried the same in Rust:
fn main() {
'Z' - 'A';
}
but the compiler complains:
error[E0369]: binary operation `-` cannot be applied to type `char`
--> src/main.rs:2:5
|
2 | 'Z' - 'A';
| ^^^^^^^^^
|
= note: an implementation of `std::ops::Sub` might be missing for `char`
How can I do the equivalent operation in Rust?
The operation is meaningless in a Unicode world, and barely ever meaningful in an ASCII world, this is why Rust doesn't provide it directly, but there are two ways to do this depending on your use case:
'Z' as u32 - 'A' as u32
b'Z' - b'A'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With