Ćwiczenie — moduły
Teraz możesz użyć poznanych informacji na temat używania modułów w funkcjach i widoczności.
Poniższy kod zawiera kilka błędów kompilatora. Przypisanie w tym ćwiczeniu polega na pomyślnym skompilowaniu kodu bez modyfikowania main
funkcji.
mod text_processing {
mod letters {
fn count_letters(text: &str) -> usize {
text.chars().filter(|ref c| c.is_alphabetic()).count()
}
}
mod numbers {
fn count_numbers(text: &str) -> usize {
text.chars().filter(|ref c| c.is_numeric()).count()
}
}
}
fn count_letters_and_numbers(text: &str) -> (usize, usize) {
let number_of_letters = ???;
let number_of_numbers = ???;
(number_of_letters, number_of_numbers)
}
fn main() {
assert_eq!(count_letters_and_numbers("221B Baker Street"), (12, 3));
assert_eq!(count_letters_and_numbers("711 Maple Street"), (11, 3));
assert_eq!(count_letters_and_numbers("4 Privet Drive"), (11, 1));
}
To ćwiczenie można również wyświetlić pod linkiem Rust Playground.
Aby znaleźć rozwiązanie dla tego ćwiczenia, zapoznaj się z tym linkiem do witryny Rust Playground.