简体   繁体   English

如何在 Rust 中从另一个字符中减去一个字符?

[英]How do I subtract one character from another in Rust?

In Java, I could do this.在 Java 中,我可以做到这一点。

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

I have tried the same in Rust:我在 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?如何在 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:该操作在 Unicode 世界中毫无意义,而在 ASCII 世界中几乎没有意义,这就是 Rust 不直接提供它的原因,但根据您的用例,有两种方法可以做到这一点:

  • Cast the characters to their scalar value: 'Z' as u32 - 'A' as u32将字符转换为其标量值: 'Z' as u32 - 'A' as u32
  • Use byte character literals: b'Z' - b'A'使用字节字符文字: b'Z' - b'A'

Math is not meaningless in unicode, that misses the most amazing feature of utf-8.数学在 unicode 中并非毫无意义,它错过了 utf-8 最令人惊奇的特性。

Any 7bit char with 0 high bit is valid us-ascii, a 7bit us-ascii doc is valid utf-8.任何高位为 0 的 7 位字符都是有效的 us-ascii,一个 7 位的 us-ascii doc 是有效的 utf-8。 You can treat utf-8 as us-ascii bytes provided all comparisons and math deal with values lower than 127. This is by design of utf-8, C code tends to just work, however rust makes this complicated.您可以将 utf-8 视为 us-ascii 字节,前提是所有比较和数学处理都处理低于 127 的值。这是 utf-8 的设计,C 代码往往可以正常工作,但是 rust 使这变得复杂。

Given a string value: &str给定一个字符串value: &str

Grab the bytes as_bytes()抓取字节as_bytes()

for byt in value.as_bytes() {
    let mut c = *byt; // c is u8 (unsigned byte)

    // if we are dealing with us-ascii chars...
    if c >= b'A' && c <= b'Z' {
        // math works, this converts to us-ascii lowercase
        c = c + 32;  
    }

    // to treat the u8 as a rust char cast it
    let ch = c as char;
    // now you can write sane code like  
    if ch == ':' || ch == ' ' || ch == '/' {
        ....
    // but you cant do math anymore

This math is not meaningless, +32 is a handy lowercase function for AZ and this is valid treatment of utf-8 chars.这个数学不是毫无意义的, +32是 AZ 的一个方便的小写函数,这是对 utf-8 字符的有效处理。

It is not by accident that a + 1 = b in utf-8. utf-8 中的a + 1 = b并非偶然。 Ascii-beticaly ordering may not be the same as real world alphabetical ordering, but it is still useful because it performs well over a common range of characters. Ascii-beticaly 排序可能与现实世界的字母排序不同,但它仍然很有用,因为它在常见的字符范围内表现良好。

It is not meaningless that '3' + 1 = '4' in ascii. ascii 中的'3' + 1 = '4'并非毫无意义。

You will not break strings utf-8 as bytes, simple code like if (c == 'a') will work even if you have smiley poos in the string.您不会将字符串 utf-8 分解为字节,即使字符串中有笑脸便便,像if (c == 'a')这样的简单代码也可以工作。

Doing math on Rust's char is impossible, which is a shame.在 Rust 的 char 上做数学是不可能的,这是一种耻辱。

Doing math on one byte of a utf-8 string is as valid as its ever been.对 utf-8 字符串的一个字节进行数学运算与以往一样有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Rust的BigInt中减去1? - How does one subtract 1 from a BigInt in Rust? 如何在 Rust 2015 中从一个模块到另一个模块进行基本的函数导入/包含? - How do I do a basic import/include of a function from one module to another in Rust 2015? 如何从 Rust 中的另一个字符串中删除单个尾随字符串? - How do I remove a single trailing string from another string in Rust? 如何在 rust 的可变间隔上运行 function,其中可以从另一个线程更改间隔? - How do I run a function on a changeable interval in rust, where the interval can be changed from another thread? 如何在 Rust 货物项目中使用另一个模块中的一个模块? - How to use one module from another module in a Rust cargo project? 如何删除 Rust 中另一个向量中出现的向量元素? - How do I remove the elements of vector that occur in another vector in Rust? 在Rust中如何将分歧函数作为参数传递给另一个函数 - In Rust how do I pass a diverging function as parameter to another function 如何在Rust中检查字符是否是Unicode换行符(不仅是ASCII)? - How do I check if a character is a Unicode new-line character (not only ASCII) in Rust? 在锈病中我该如何在一行中进行此类型转换 - In rust how can i do this type conversion in one line 如何在 Rust 中显示带有 ncurses 的字符? - How do you display a character with ncurses in Rust?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM