How to create a static array of strings?

前端 未结 4 1301
情书的邮戳
情书的邮戳 2021-02-03 18:45

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

How do

4条回答
  •  萌比男神i
    2021-02-03 19:41

    There are two related concepts and keywords in Rust: const and static:

    https://doc.rust-lang.org/reference/items/constant-items.html

    For most use cases, including this one, const is more appropriate, since mutation is not allowed, and the compiler may inline const items.

    const STRHELLO:&'static str = "Hello";
    const STRWORLD:&'static str = "World";
    const ARR:[&'static str, ..2] = [STRHELLO,STRWORLD];
    

    Note, there is some out-of-date documentation out there that doesn't mention the newer const, including Rust by Example.

提交回复
热议问题