Rust – il linguaggio – 31

Ccw

Da qui un altro passo verso la meta, oggi copio qui: /usr/local/share/doc/rust/html/book/const-and-static.html.

const e static

Rust has a way of defining constants with the const keyword:

const N: i32 = 5;

Unlike let bindings, you must annotate the type of a const.

Constants live for the entire lifetime of a program. More specifically, constants in Rust have no fixed address in memory. This is because they’re effectively inlined to each place that they’re used. References to the same constant are not necessarily guaranteed to refer to the same memory address for this reason.

static

Rust provides a ‘global variable’ sort of facility in static items. They’re similar to constants, but static items aren’t inlined upon use. This means that there is only one instance for each value, and it’s at a fixed location in memory.

Here’s an example:

static N: i32 = 5;

Unlike let bindings, you must annotate the type of a static.

Statics live for the entire lifetime of a program, and therefore any reference stored in a constant has a 'static lifetime:

fn main() {
    static NAME: &'static str = "Juhan";
    println!("Ciao {}!", NAME);    
}

rs31-0

Mutabilità

You can introduce mutability with the mut keyword:

static mut N: i32 = 5;

Because this is mutable, one thread could be updating N while another is reading it, causing memory unsafety. As such both accessing and mutating a static mut is unsafe, and so must be done in an unsafe block:

fn main() {
    static mut N: i32 = 7;
    
    unsafe {
        N *= 6;
        println!("N: {}", N);
    }
}

rs31-1

Ovviamente…

fn main() {
    static mut N: i32 = 7;
    
    unsafe {
        N *= 6;
        println!("N: {}", N);
    }
    println!("fuori dal blocco precedente N: {}", N);
}

rs31-2OOPS!, certo:

fn main() {
    static mut N: i32 = 7;
    
    unsafe {
        N *= 6;
        println!("N: {}", N);
    }
    unsafe {
        println!("fuori dal blocco precedente N: {}", N);
    } 
}

rs31-3

Ahemmm… 🙄

Furthermore, any type stored in a static must be Sync, and may not have a Drop [/usr/local/share/doc/rust/html/book/drop.html] implementation.

Inizializzazione

Both const and static have requirements for giving them a value. They may only be given a value that’s a constant expression. In other words, you cannot use the result of a function call or anything similarly complex or at runtime.

Che construtto si può usare?

Almost always, if you can choose between the two, choose const. It’s pretty rare that you actually want a memory location associated with your constant, and using a const allows for optimizations like constant propagation not only in your crate but downstream crates.
Sì, sensato 😀
:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: