在Java中,我可以做到这一点.
int diff = 'Z' - 'A'; // 25
Run Code Online (Sandbox Code Playgroud)
我在Rust中尝试了同样的方法:
fn main() {
'Z' - 'A';
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨:
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`
Run Code Online (Sandbox Code Playgroud)
如何在Rust中执行等效操作?
在Unicode世界中,该操作毫无意义,并且在ASCII世界中几乎没有任何意义,这就是Rust不直接提供它的原因,但根据您的使用情况,有两种方法可以执行此操作:
'Z' as u32 - 'A' as u32b'Z' - b'A'| 归档时间: |
|
| 查看次数: |
276 次 |
| 最近记录: |