-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function for capitalizing strings #1375
Conversation
src/function.rs
Outdated
let mut chars = s.chars(); | ||
match chars.next() { | ||
None => Ok(String::new()), | ||
Some(c) => Ok(c.to_uppercase().collect::<String>() + | ||
&chars.as_str().to_lowercase()), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this:
let mut chars = s.chars(); | |
match chars.next() { | |
None => Ok(String::new()), | |
Some(c) => Ok(c.to_uppercase().collect::<String>() + | |
&chars.as_str().to_lowercase()), | |
} | |
Ok(s.chars().enumerate().flat_map(|(i, c)| if i == 0 { c.upper_case() } else { c.lower_case() }).collect()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So much more elegant! Applied with rustfmt and some modifications.
Good feature, and thanks for also adding a test! Very minor comment suggesting a slightly tweaked implementation. |
Also, could you add a note, like this to the function documentation: |
Function `capitalize` converts first character of given string to uppercase and the rest to lowercase.
Merged! Thank you for the PR! |
Hello,
This PR adds a
capitalize
function (name inspired by the Python string method) for converting a string to sentence case; first letter upper case, rest lower case.I have a use case where a function like this would be useful and it's a small enough change so I'm creating this PR without a discussion issue but it's not a big loss if it doesn't get merged.
Thanks!