Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let s = "привет";
println!("{} {}", s.len(), s.chars().count());
}12 6
- Expecting
lento count letters: it counts bytes, and each Cyrillic letter is 2 bytes in UTF-8, so «привет» has length 12. - Expecting a string to store each letter as a 4-byte
char: a&strholds UTF-8, where a letter takes 1 to 4 bytes.
- What would the program print for the word
"hello", and why? - Why does
chars().count()take O(n) time whilelentakes O(1)?