How do I subtract one character from another in Rust?

后端 未结 1 728
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 18:51

In Java, I could do this.

int diff = \'Z\' - \'A\'; // 25

I have tried the same in Rust:



        
相关标签:
1条回答
  • 2021-01-17 19:04

    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:

    • Cast the characters to their scalar value: 'Z' as u32 - 'A' as u32
    • Use byte character literals: b'Z' - b'A'
    0 讨论(0)
提交回复
热议问题