-
-
Save jpastuszek/2704f3c5a3864b05c48ee688d0fd21d7 to your computer and use it in GitHub Desktop.
Rust: title case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Author
jpastuszek
commented
Jun 17, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment