1

Does anyone know a function that changes the first letter of a String to the uppercase equivalent? Idealy, it would be used as so:

let newfoo = first_letter_to_uppper_case("foobar".to_string()), or
let newfoo = "foobar".to_string().first_letter_to_uppper_case().

Joel Ellis
  • 1,332
  • 1
  • 12
  • 31
  • no, I'm asking for a How, not a Why. These are rather diferent questions, and the answer involving using a crate requires building its [26 dependancies](https://crates.io/crates/inflector/reverse_dependencies). adding that many dependancies is not a quick copy paste snippet of code. – Joel Ellis Dec 01 '18 at 13:32
  • The answer of Shep that yourself quote answer both how and why. – Stargateur Dec 01 '18 at 13:33
  • Ah, yes. I see it now. TBH i didn't consider it would be working code, given that he states it isn't code he'd use/write and it was what it *looked* like, and he'd rather use crates.io. Time to edit. – Joel Ellis Dec 01 '18 at 13:39
  • 2
    I've [added an answer](https://stackoverflow.com/a/53571882/3650362) to the other question that may suit your use case. – trent Dec 01 '18 at 14:44

1 Answers1

-2

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.

Joel Ellis
  • 1,332
  • 1
  • 12
  • 31
  • 1
    Sorry, but this answer is plenty wrong you assume the "letter" is only one bytes, remember that String in Rust are UTF-8 String, convert the first character is not as trivial as you think. – Stargateur Dec 01 '18 at 12:55
  • See my latest edit, also, it is meant to be 'quick'. ;) – Joel Ellis Dec 01 '18 at 12:57
  • 1
    Well, In your implementation you create twice a string, that not what I would call quick also quick doesn't mean wrong and for finish I think that the answer you add is a the answer to this question. – Stargateur Dec 01 '18 at 13:05
  • by *quick*, I mean speed of writing quick. quick doesn't meen wrong *or* right. My use for this is to capitalise the first word in a Lorem Ipsum sentance. It's intended to replace Javascript's `sentence = sentence.charAt(0).toUpperCase() + sentence.slice(1);`. It's good enough for it's job. – Joel Ellis Dec 01 '18 at 13:21
  • It's now *correct*. – Joel Ellis Dec 01 '18 at 13:46
  • You don't have to call `.collect::()` you can just call `.to_string()` – Evan Carroll Jun 13 '20 at 17:07