pub trait StringExt {
fn is_lowercase(&self) -> bool;
fn starts_with_lowercase(&self) -> bool;
fn to_lowercase_first(&self) -> String;
fn to_uppercase_first(&self) -> String;
}
impl StringExt for str {
fn is_lowercase(&self) -> bool {
self.chars().all(|c| c.is_lowercase())
}
fn starts_with_lowercase(&self) -> bool {
match self.chars().next() {
None => false,
Some(c) => c.is_lowercase(),
}
}
fn to_lowercase_first(&self) -> String {
self.chars()
.enumerate()
.map(|(i, c)| {
if i == 0 {
c.to_lowercase().collect::<String>()
} else {
c.to_string()
}
})
.collect::<String>()
}
fn to_uppercase_first(&self) -> String {
self.chars()
.enumerate()
.map(|(i, c)| {
if i == 0 {
c.to_uppercase().collect::<String>()
} else {
c.to_string()
}
})
.collect::<String>()
}
}