ChatGPT解决这个技术问题 Extra ChatGPT

Get the String length in characters in Rust

Based on the Rust book, the String::len method returns the number of bytes composing the string, which may not correspond to the length in characters.

For example if we consider the following string in Japanese, len() would return 30, which is the number of bytes and not the number of characters, which would be 10:

let s = String::from("ラウトは難しいです!");
s.len() // returns 30.

The only way I have found to get the number of characters is using the following function:

s.chars().count()

which returns 10, and is the correct number of characters.

Is there any method on String that returns the characters count, aside from the one I am using above?

Note that given Unicode idiosyncrasies, "number of characters" probably doesn't mean what you think it does. For example, this string: "é" has two characters as evidenced in the playground: play.rust-lang.org/…, although this string: "é" only has one character: play.rust-lang.org/…

S
Shepmaster

Is there any method on String that returns the characters count, aside from the one I am using above?

No. Using s.chars().count() is correct. Note that this is an O(N) operation (because UTF-8 is complex) while getting the number of bytes is an O(1) operation.

You can see all the methods on str for yourself.

As pointed out in the comments, a char is a specific concept:

It's important to remember that char represents a Unicode Scalar Value, and may not match your idea of what a 'character' is. Iteration over grapheme clusters may be what you actually want.

One such example is with precomposed characters:

fn main() {
    println!("{}", "é".chars().count()); // 2
    println!("{}", "é".chars().count()); // 1
}

You may prefer to use graphemes from the unicode-segmentation crate instead:

use unicode_segmentation::UnicodeSegmentation; // 1.6.0

fn main() {
    println!("{}", "é".graphemes(true).count()); // 1
    println!("{}", "é".graphemes(true).count()); // 1
}

BTW ˋs.chars().count()ˋ is the number of unicode codepoints, you can use unicode-segmentation to split on graphemes.
@Shepmaster, thank you fro your answer. I knew the chars and String are different, as you can guess from my question. I was simply wondering if there was a more efficient, and intuitive way to do it.
@GrégoryOBANOS thank you for your comment, but I am not planning to install anything for something that should be simple.
@SalvatoreCosentino Bluntly put, counting the characters in a string is not simple (see also Why is capitalizing the first letter of a string so convoluted in Rust?) and you will be greatly disserviced if you avoid using Rust crates. Many programmers are under the wrong impression that dealing with natural language should be "easy", allowing many programs to simply get it wrong. Rust is trying very hard to avoid that fate.
@JerzyBrzóska I think you are experiencing How to ignore the line break while printing a string read from stdin?

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now