如何创建一个静态的字符串数组? [英] How to create a static array of strings?

查看:504
本文介绍了如何创建一个静态的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:该问题包含Rust 1.0之前的语法.该代码无效,但概念仍然相关.

Note This question contains syntax that predates Rust 1.0. The code is invalid, but the concepts are still relevant.

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

How do you create a global static array of strings in Rust?

对于整数,它将编译:

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

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

But I can't get something similar for strings to compile:

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及其后续版本的稳定替代方案:

This is a stable alternative for Rust 1.0 and every subsequent version:

const BROWSERS: &'static [&'static str] = &["firefox", "chrome"];

这篇关于如何创建一个静态的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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