Halo
发布于 2024-08-28 / 37 阅读 / 0 评论 / 0 点赞

rust模块

定义模块

顶层模块

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

子模块

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

或者

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

评论