如何创建静态字符串数组?

2024-04-28

Note这个问题包含 Rust 1.0 之前的语法。代码无效,但概念仍然相关。

如何在 Rust 中创建全局静态字符串数组?

对于整数,编译如下:

static ONE:u8 = 1;
static TWO:u8 = 2;
static ONETWO:[&'static u8, ..2] = [&ONE, &TWO];

但我无法得到类似的字符串来编译:

static STRHELLO:&'static str = "Hello";
static STRWORLD:&'static str = "World";
static ARR:[&'static str, ..2] = [STRHELLO,STRWORLD]; // Error: Cannot refer to the interior of another static

这是 Rust 1.0 和每个后续版本的稳定替代品:

const BROWSERS: &'static [&'static str] = &["firefox", "chrome"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何创建静态字符串数组? 的相关文章

随机推荐