The English alphabet as a vector of characters in Rust

后端 未结 2 1463
野趣味
野趣味 2021-01-13 07:03

The title says it all. I want to generate the alphabet as a vector of characters. I did consider simply creating a range of 97-122 and converting it to characters, but I was

2条回答
  •  抹茶落季
    2021-01-13 08:01

    Hard-coding this sort of thing makes sense, as it can then be a compiled constant, which is great for efficiency.

    static ASCII_LOWER: [char; 26] = [
        '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',
    ];
    

    (Decide for yourself whether to use static or const.)

    This is pretty much how Python does it in string.py:

    lowercase = 'abcdefghijklmnopqrstuvwxyz'
    # ...
    ascii_lowercase = lowercase
    

提交回复
热议问题