Strings In Depth16 / 18
Up next

Accept any string-like type

// AsRef<str> accepts &str, String, &String, Box<str>, ...
fn word_count(text: impl AsRef<str>) -> usize {
    text.as_ref().split_whitespace().count()
}

fn main() {
    println!("{}", word_count("a b c"));
    println!("{}", word_count(String::from("x y")));
}
What you're solving

impl AsRef<str> is the flexible param