Complete the code to check if a word starts with the given prefix.
if word.[1](prefix) { count += 1; }
The starts_with method checks if the string begins with the given prefix.
Complete the code to iterate over all words in the list.
for [1] in words.iter() {
We use word as the variable name to represent each element in the iteration.
Fix the error in the code to convert the prefix to a string slice.
let prefix_str = &prefix[1]();The as_str method converts a String to a string slice &str without copying.
Fill both blanks to create a function that counts words starting with the prefix.
fn count_words_with_prefix(words: &[String], prefix: &str) -> usize {
let mut count = 0;
for [1] in words.iter() {
if [2].starts_with(prefix) {
count += 1;
}
}
count
}We iterate over each word in words and check if word starts with the prefix.
Fill all three blanks to create a main function that tests counting words with a prefix.
fn main() {
let words = vec!["apple".to_string(), "app".to_string(), "banana".to_string()];
let prefix = "app";
let result = count_words_with_prefix([1], [2]);
println!("Count: {}", [3]);
}We pass a reference to words and the prefix to the function, then print the result.
