Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get A Vec Of Uppercase Or Lowercase English Letters In Rust

Python has `ascii_lowercase``^lc^^ and `ascii_uppercase``^uc^^ 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.

Code

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()
}

Results

[_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",
]

Code

('a'..='z').into_iter().collect::<Vec<char>>()

Footnotes