Skip to content

Instantly share code, notes, and snippets.

@jpastuszek
Forked from anonymous/playground.rs
Created August 30, 2016 09:59
Show Gist options
  • Save jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7 to your computer and use it in GitHub Desktop.
Save jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7 to your computer and use it in GitHub Desktop.
Rust: title case
fn title_case(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().chain(c.flat_map(|t| t.to_lowercase())).collect(),
}
}
fn main() {
let s = "foo-bar_baz-QUAX";
let us = s.split('-').map(|v| title_case(v)).collect::<Vec<_>>().join("-");
println!("{}", us);
}
@jpastuszek
Copy link
Author

jpastuszek commented Jun 17, 2017

    for word in "The quick brown fox jumps over the lazy dog"
        .split_whitespace() 
        .map(|w| w.chars())
        .map(|mut c| 
            c.next().into_iter()
            .flat_map(|c| c.to_uppercase())
            .chain(c.flat_map(|c| c.to_lowercase())))
        .map(|c| c.collect::<String>()) 
    {
        print!("{:?} ", word); // "The" "Quick" "Brown" "Fox" "Jumps" "Over" "The" "Lazy" "Dog" 
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment