Skip to content
rust模块
2025年3月13日 root

定义模块

顶层模块

  1. src/main.rs
rust
mod  controller;
mod  router;
fn  main() {
    // do something
}
  1. 模块对应文件夹或文件 |--- src |--- controller |--- router |--- main.rs

子模块

  1. src/controller/mod.rs
rust
pub(crate) mod  news;
pub(crate) mod  auth;
pub(crate) mod  stats;

或者

rust
mod  news;
mod  auth;
mod  stats;
pub(crate) use  news::Tag;
pub(crate) use  auth::User;
  1. 模块对应文件夹或文件 |--- src |--- controller |--- mod.rs |--- auth |--- news |--- stats

使用模块

  1. 在定义模块文件中,如main.rs 直接使用即可,例如:
rust
mod  router;
// something
fn  main() {
zino::Cluster::boot()
.register(router::routes())
// something
}
  1. 非定义模块文件中
rust
use  crate::controller::{stats, news};
let var = news::function();

Last updated: