Rust – il linguaggio – 30

pr1

Continuando da qui oggi sono qui: /usr/local/share/doc/rust/html/book/crates-and-modules.html al tag #complex-imports.

Imports complessi

Rust offers several advanced options that can add compactness and convenience to your extern crate and use statements. Here is an example:

extern crate phrases as sayings;

use sayings::japanese::greetings as ja_greetings;
use sayings::japanese::farewells::*;
use sayings::english::{self, greetings as en_greetings, farewells as en_farewells};

fn main() {
    println!("Hello in English; {}", en_greetings::hello());
    println!("And in Japanese: {}", ja_greetings::hello());
    println!("Goodbye in English: {}", english::farewells::goodbye());
    println!("Again: {}", en_farewells::goodbye());
    println!("And in Japanese: {}", goodbye());
}

First, both extern crate and use allow renaming the thing that is being imported. So the crate is still called “phrases“, but here we will refer to it as “sayings“. Similarly, the first use statement pulls in the japanese::greetings module from the crate, but makes it available as ja_greetings as opposed to simply greetings. This can help to avoid ambiguity when importing similarly-named items from different places.

The second use statement uses a star glob to bring in all symbols from the sayings::japanese::farewells module. As you can see we can later refer to the Japanese goodbye function with no module qualifiers. This kind of glob should be used sparingly.

The third use statement bears more explanation. It’s using “brace expansion” globbing to compress three use statements into one (this sort of syntax may be familiar if you’ve written Linux shell scripts before). The uncompressed form of this statement would be:

fn main() {
    use sayings::english;
    use sayings::english::greetings as en_greetings;
    use sayings::english::farewells as en_farewells;
}

As you can see, the curly brackets compress use statements for several items under the same path, and in this context self just refers back to that path. Note: The curly brackets cannot be nested or mixed with star globbing.

Nota: continuo ad avere problemi di compilazione; crates e moduli restano per me da rivedere in un prossimo futuro, quando sarò meno niubbo con Rust.
: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: