The vowel counter in the crate returns 1 for "café". It should return 2. There are two vowels in that word, and one of them does not get counted.
The set it compares against is five characters:
let vowels = ['a', 'e', 'i', 'o', 'u'];
And the comparison is one call:
s.chars()
.filter(|c| vowels.contains(&c.to_ascii_lowercase()))
.count() as i32
to_ascii_lowercase is where the assumption lives. The name says ASCII, and it means it: the method folds A to a and leaves every other character exactly as it found it. I checked the boundaries:
'É' (U+00C9) -> to_ascii_lowercase = 'É' (U+00C9), is_ascii=false
'é' (U+00E9) -> to_ascii_lowercase = 'é' (U+00E9), is_ascii=false
'A' (U+0041) -> to_ascii_lowercase = 'a' (U+0061), is_ascii=true
So a string with an accented vowel in it does not count that vowel.
"ÉCOLE" returns 2, counting the E and the O and skipping the É. "naïve" returns 2.
The problem statement says the string consists of printable ASCII characters, so inside the stated constraints the ASCII-only fold is exactly right, and a Unicode-aware fold would be work the problem does not ask for. The important thing is that the assumption is named in the method rather than hidden in a comparison table or a Unicode normalization step nobody would notice.
The wide version has a different type
The alternative is char::to_lowercase, which does the full Unicode mapping. It has a different type: it returns an iterator over the lowercase forms, not a single char, because a character’s lowercase mapping is not always a single character. My own experiment, collecting that iterator:
let lowered: String = 'É'.to_lowercase().collect();
collects to "é", one character, but the compiler cannot promise that in general. U+0130 lowercases to two code points, U+0069 and U+0307. A to_lowercase version of the filter would have to cope with the possibility of more than one lowercase form per character, and the comparison would stop being a plain char equality.
The ASCII version returns char in, char out, and the whole comparison stays a single char equality.
That is the trade. The narrow method gives a narrow, fast, predictable answer. The wide method gives a wide answer, and the folding it needs is not a single-character operation.
One character, two bytes
The iteration is s.chars(), not s.bytes(). Over the same string they disagree about length:
"café".chars().count() = 4
"café".bytes().count() = 5
"café".bytes() = [99, 97, 102, 195, 169]
é is one character and two bytes: 195 and 169. A byte-oriented version of this function would count the bytes, and casting a byte back to a char does not give you the character:
byte 0xE9 as char = Some('Ã')
195 is the lead byte of a two-byte UTF-8 sequence and 169 is the continuation byte. Read as Latin-1 they spell é instead of é.
The byte-oriented version of a vowel counter does not count the wrong vowels, it counts nonsense. chars() decodes UTF-8 properly and hands back the characters, so the ASCII fold lands on an actual é rather than on a fragment of its encoding.
Five elements, then the cast
vowels.contains(&c.to_ascii_lowercase()) is a linear scan over five elements, and it short-circuits on a hit. Over "café" that came to 16 comparisons across four characters, so it averages four per character rather than five.
A match would let the compiler pick a different strategy for the same question. At five elements the difference is noise, and the array version reads closer to the specification.
The & in contains(&c.to_ascii_lowercase()) is there because contains takes a reference. c is already &char inside the closure, and to_ascii_lowercase consumes by value, so the method call auto-derefs and produces a char, and the & takes a reference to that temporary.
Then the cast.
count() returns usize, which is 8 bytes on this platform, and i32 is 4:
count() type is usize, value = 3
size_of::<usize>() = 8, size_of::<i32>() = 4
as i32 narrows without checking. It is not a conversion that can fail; it is a truncation, and it is silent:
2147483652 as i32 = -2147483644
i32::try_from(2147483652) = Err(TryFromIntError(PosOverflow))
A count of vowels can never reach that magnitude, because the count is bounded by the number of characters in the string and the problem caps that at 10^4. So the unchecked cast is safe here for a reason that has nothing to do with the cast itself: the input is small. i32::try_from would say so out loud instead of relying on a constraint stated elsewhere in the problem.
Both versions are still in the file
The file keeps the plain loop above the iterator version, commented out:
// iterative version
let vowels = ['a', 'e', 'i', 'o', 'u'];
// let mut count = 0;
// for ch in s.chars() {
// if vowels.contains(&ch.to_ascii_lowercase()) {
// count += 1;
// }
// }
// count
// rusty version
s.chars()
.filter(|c| vowels.contains(&c.to_ascii_lowercase()))
.count() as i32
The file labels the active one “rusty version” and leaves the other in place. Both do the same work. The loop shows the counter; the iterator chain says “keep the characters that pass this test” and lets count do the tallying.
Neither is meaningfully faster than the other in any way that matters at this size.
What the pair does not show is a version that handles the rest of the alphabet. to_ascii_lowercase is the reason it fits in one line, and the assumption behind it is that the input is ASCII.
The habit I keep from this: read the method name before you read the comparison. It already said ASCII. I was reading past it.