If you want a function used as so:
let newfoo = first_letter_to_uppper_case("foobar".to_string())
Try use the following:
fn main() {
println!("{}", first_letter_to_uppper_case("foobar".to_string()));
}
fn first_letter_to_uppper_case (s1: String) -> String {
let mut c = s1.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
If you want it as a function implimented on the string type, like let newfoo = "foobar".to_string().first_letter_to_uppper_case()
, try:
pub trait FirstLetterToUppperCase {
fn first_letter_to_uppper_case(self) -> String;
}
impl FirstLetterToUppperCase for String {
fn first_letter_to_uppper_case(self) -> String {
let mut c = self.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
}
fn main() {
println!("{}", "foobar".to_string().first_letter_to_uppper_case());
}
However, these functions do not deal with non-ascii characters very well. For more information, see this answer.