Rust - Grundlagen - 07 - Package Crate Module

Package Crate Module

Verständnis

Ein Package darf höchstens ein Library-Crate, aber beliebig viele Binary-Crates enthalten.

  • module pub

    mod front_of_house {
        // 其他 module 引用这里的嵌套 module,需要有 pub 修饰
        pub mod hosting {
            // 其他 module 引用这里的 fn 也需要有 pub 修饰
            pub fn add_to_waitlist() {}
        }
    
        // 引用嵌套 struct 也需要有 pub 修饰
        pub struct Breakfast {
            // struct 内的 field 默认都是 private,外部访问也需要 pub 修饰
            pub toast: String,
            seasonal_fruit: String,
        }
    
        impl Breakfast {
            pub fn summer(toast: &str) -> Breakfast {
                Breakfast {
                    toast: String::from(toast),
                    seasonal_fruit: String::from("peaches"),
                }
            }
        }
    }
    
    pub fn eat_at_restaurant() {
        // Absolute path
        crate::front_of_house::hosting::add_to_waitlist();
    
        // Relative path
        front_of_house::hosting::add_to_waitlist();
    }
    
  • module use

    // 类似于 C++ 的 using namespace
    mod front_of_house {
        pub mod hosting {
            pub fn add_to_waitlist() {}
        }
    }
    use crate::front_of_house::hosting;
    pub fn eat_at_restaurant() {
        hosting::add_to_waitlist();
        hosting::add_to_waitlist();
        hosting::add_to_waitlist();
    }
    
    // 支持 use 的时候用 pub 修饰,也称 Re-exporting,外部使用时可直接 hosting::xxx
    pub use crate::front_of_house::hosting;
    
    // 可以简化 use
    use std::cmp::Ordering;
    use std::io;
    use std::{cmp::Ordering, io};
    
    // 简化时可以用 self
    use std::io;
    use std::io::Write;
    use std::io::{self, Write};
    
    // 可以用 * 引入所有 module
    use std::collections::*;
    

Bei mod a; liest Rust die Datei oder den Ordner a im selben Verzeichnis.

Quelle

https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html

…

Ein Package besteht aus einem oder mehreren Crates, die gemeinsam Funktionalität bereitstellen. Es enthält eine Cargo.toml-Datei, die beschreibt, wie diese Crates gebaut werden.

Für den Inhalt eines Packages gelten einige Regeln. Es darf höchstens ein Library-Crate enthalten, aber beliebig viele Binary-Crates. Mindestens ein Crate (Library oder Binary) ist erforderlich.

…

Separating Modules into Different Files

Bisher definierten alle Beispiele dieses Kapitels mehrere Module in einer Datei. Werden Module groß, kann man ihre Definitionen in eine eigene Datei verschieben, damit der Code leichter zu navigieren ist.

Wir verschieben beispielsweise das Modul front_of_house aus Listing 7-17 in die Datei src/front_of_house.rs, indem die Crate-Root-Datei den Code aus Listing 7-21 enthält. Hier ist die Crate-Root-Datei src/lib.rs; das Verfahren funktioniert auch bei Binary-Crates mit src/main.rs.

Dateiname: src/lib.rs


mod front_of_house;

pub use crate::front_of_house::hosting;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
}

Listing 7-21: Deklaration des Moduls front_of_house, dessen Inhalt in src/front_of_house.rs steht

src/front_of_house.rs enthält die Definitionen des Moduls front_of_house, wie Listing 7-22 zeigt.

Dateiname: src/front_of_house.rs


pub mod hosting {
    pub fn add_to_waitlist() {}
}

Listing 7-22: Definitionen im Modul front_of_house in src/front_of_house.rs

Ein Semikolon nach mod front_of_house statt eines Blocks weist Rust an, den Modulinhalt aus einer gleichnamigen Datei zu laden. Für das Beispiel extrahieren wir auch hosting und lassen in src/front_of_house.rs nur dessen Deklaration stehen:

Dateiname: src/front_of_house.rs


pub mod hosting;

Anschließend erstellen wir src/front_of_house directory and a file src/front_of_house/hosting.rs to contain the definitions made in the hosting module:

Dateiname: src/front_of_house/hosting.rs


pub fn add_to_waitlist() {}

Der Modulbaum bleibt gleich, und die Funktionsaufrufe in eat_at_restaurant funktionieren ohne Änderungen, obwohl die Definitionen in verschiedenen Dateien liegen. So lassen sich wachsende Module in neue Dateien verschieben.

Beachte, dass die pub use crate::front_of_house::hosting Anweisung in src/lib.rs ebenfalls unverändert bleibt; außerdem beeinflusst use nicht, welche Dateien als Teil der Crate kompiliert werden. The mod keyword declares modules, and Rust looks in a file with the same name as the module for the code that goes into that module.