英文字母表作为 Rust 中的字符向量 [英] The English alphabet as a vector of characters in Rust

查看:45
本文介绍了英文字母表作为 Rust 中的字符向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切.我想将字母表生成为字符向量.我确实考虑过简单地创建一个 97-122 的范围并将其转换为字符,但我希望有一种更好看的方式,例如 Python 的 string.ascii_lower.

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 hoping there would be a nicer looking way, such as Python's string.ascii_lower.

结果向量或字符串应包含字符 a-z.

The resulting vector or string should have the characters a-z.

推荐答案

硬编码这种事情是有道理的,因为它可以是一个编译常量,这对效率很有帮助.

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',
];

(自行决定是使用 static 还是 const.)

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

这几乎就是 Python 在 string.py 中的做法:

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

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

这篇关于英文字母表作为 Rust 中的字符向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆