Hoe maak je een statische reeks strings aan?

OpmerkingDeze vraag bevat een syntaxis die ouder is dan Rust 1.0. De code is ongeldig, maar de concepten zijn nog steeds relevant.

Hoe maak je een globale statische array van strings in Rust?

Voor gehele getallen compileert dit:

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

Maar ik kan niet iets soortgelijks krijgen om strings te compileren:

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

Antwoord 1, autoriteit 100%

Dit is een stabiel alternatief voor Rust 1.0 en elke volgende versie:

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

Antwoord 2, autoriteit 8%

Er zijn twee verwante concepten en trefwoorden in Rust: const en static:

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

Voor de meeste gevallen, waaronder deze, is const geschikter, aangezien mutatie niet is toegestaan, en de compiler const-items inline kan plaatsen.

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

Let op, er is wat verouderde documentatie die de nieuwere const niet vermeldt, inclusief Rust door Voorbeeld.


Antwoord 3, autoriteit 5%

Een andere manier om het tegenwoordig te doen is:

const A: &'static str = "Apples";
const B: &'static str = "Oranges";
const AB: [&'static str; 2] = [A, B]; // or ["Apples", "Oranges"]

Antwoord 4, autoriteit 3%

Ik heb dit zojuist gebruikt om een ​​klein POC-niveau toe te wijzen aan een spel in Rust

const LEVEL_0: &'static [&'static [i32]] = &[
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 9, 0, 0, 0, 2, 0, 0, 3, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];

En geladen met de volgende functie

pub fn load_stage(&mut self, ctx: &mut Context, level: usize) {
    let levels = vec![LEVEL_0];
    for (i, row) in levels[level].iter().enumerate() {
        for (j, col) in row.iter().enumerate() {
            if *col == 1 {
                self.board.add_block(
                    ctx,
                    Vector2::<f32>::new(j as f32, i as f32),
                    self.cell_size,
                );
            }

Antwoord 5

Tegenwoordig kun je het schrijven zonder indirectheid via een aanwijzer:

const ONETWO: [u8;2]   = [1, 2];
const ARRAY:  [&str;2] = ["Hello", "World"];
fn main() {
    println!("{} {}", ONETWO[0], ONETWO[1]);  // 1 2
    println!("{} {}", ARRAY[0], ARRAY[1]);  // Hello World
}

Dankzij statische levensduurelisie hoeft u meestal niet expliciet te gebruiken

'staticzoals in:

const ARRAY: [&'static str;2] = ["Hello", "World"];

Other episodes