Home
Rust
String Count Characters
Updated Mar 6, 2022
Dot Net Perls
Count characters. Suppose you have a Rust str, and you want to count its characters like Microsoft Word. If we treat each space as a separate character, results are incorrect.
Method notes. In word processors, the logic counts sequential whitespace (like space chars) as single characters. This gives a more real-world estimate of character counts.
Example code. To begin, we introduce 2 Rust functions. The count_chars function is more interesting: it has logic to test for sequential whitespace.
Info In count_chars, we use is_whitespace() to detect space chars. Then on each char, we record whether we encountered a whitespace.
Loop, String Chars
And If the previous char was a whitespace char, and the current also is one, we do not count the current one as a character.
Finally The count_nonspace_chars is simpler—it ignores all characters that are whitespace.
fn count_chars(test: &str) -> i32 { let mut result = 0; let mut last_was_space = false; for c in test.chars() { // Only count whitespace chars that are not preceded by another whitespace. if c.is_whitespace() { if last_was_space == false { result += 1; } last_was_space = true; } else { result += 1; last_was_space = false; } } return result; } fn count_nonspace_chars(test: &str) -> i32 { let mut result = 0; for c in test.chars() { // Count all chars that are not whitespace. if !c.is_whitespace() { result += 1; } } return result; } fn main() { let test = "There is a cat, my friend. How are you?"; // Test the functions. let result1 = count_chars(test); let result2 = count_nonspace_chars(test); println!("RESULT1: {}", result1); println!("RESULT2: {}", result2); }
RESULT1: 39 RESULT2: 31
Results note. In my previous research, I found that word processor signore sequential whitespace. And the Rust program results match results of existing methods.
Tip Search for the C# Count Characters method on this site for a complete analysis of word processors.
A summary. It is possible to build functions in Rust that parallel existing software features. Having methods that can give results like this is useful.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 6, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen