Count
charactersSuppose 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.
In word processors, the logic counts sequential whitespace (like space chars) as single characters. This gives a more real-world estimate of character counts.
To begin, we introduce 2 Rust functions. The count_chars
function is more interesting: it has logic to test for sequential whitespace.
count_chars
, we use is_whitespace()
to detect space chars. Then on each char
, we record whether we encountered a whitespace.char
was a whitespace char
, and the current also is one, we do not count the current one as a character.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
In my previous research, I found that word processor signore sequential whitespace. And the Rust program results match results of existing methods.
It is possible to build functions in Rust that parallel existing software features. Having methods that can give results like this is useful.