home ~ projects ~ socials

Get A Vec Of Uppercase Or Lowercase English Letters In Rust

Python has ascii_lowercaselc and ascii_uppercaseuc to grab quick sets of ASCII letters. I haven't found anything like that in the Rust standard library. There's probably a crate that does it, but my quick search didn't turn up anything well maintained. The approach I'm using is to loop through the ASCII character codes to get the letters. Uppercase is 65-90 and lowercase is 97-122.

fn main() {
  dbg!(upper_case_letters());
  dbg!(lower_case_letters());
}

fn upper_case_letters() -> Vec<String> {
  (65..=90).map(|c| char::from_u32(c).unwrap().to_string()).collect()
}

fn lower_case_letters() -> Vec<String> {
  (97..=122).map(|c| char::from_u32(c).unwrap().to_string()).collect()
}
Output:
[_active_nvim_run:2:3] upper_case_letters() = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z",
]
[_active_nvim_run:3:3] lower_case_letters() = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
]

Since writing this I found a StackOverflow answer that uses this appraoch. (TODO: test that and write it up)

('a'..='z').into_iter().collect::<Vec<char>>()
-- end of line --

Footnotes

lc ⤴
uc ⤴
so ⤴