How to check if a string is title case in Rust

1 Answer

0 votes
fn is_title_case(s: &str) -> bool {
    for word in s.split_whitespace() {
        let mut chars = word.chars();
        match chars.next() {
            Some(first) if first.is_uppercase() => (),
            _ => return false,
        }
        if chars.any(|c| !c.is_lowercase()) {
            return false;
        }
    }
    true
}

fn main() {
    println!("{}", is_title_case("Hello World")); // true
    println!("{}", is_title_case("Hello world")); // false
}



/*
run:

true
false

*/

 



answered 3 hours ago by avibootz
...