Backend
rust-project-structure
Rust 项目结构与脚手架技能 — 包(package)与 crate 组织、模块系统深度(文件模块树、mod 声明、pub 可见性体系)、use 路径模式、工作空间布局、项目模板。基于 The Book ch 7 与 The Reference ch 7。当用户需要组织项目结构、管理模块关系、搭建多包项目时激活。
Rust 项目结构与脚手架技能 — 包(package)与 crate 组织、模块系统深度(文件模块树、mod 声明、pub 可见性体系)、use 路径模式、工作空间布局、项目模板。基于 The Book ch 7 与 The Reference ch 7。当用户需要组织项目结构、管理模块关系、搭建多包项目时激活。
基于 The Rust Programming Language ch 7 与 The Rust Reference ch 7(Items & Modules)。
A::{B,C}、glob A::*、别名 A as B、re-export)rust-cargo-build 技能rust-1.93 技能rust-testing 技能本技能不收集、存储或传输任何用户数据。
// package 二进制 + 库混合布局
my-project/
├── Cargo.toml
├── src/
│ ├── lib.rs # 库 crate 根
│ └── main.rs # 二进制 crate 根
// 多二进制 crate 布局
my-project/
├── Cargo.toml
└── src/
├── lib.rs
├── main.rs
└── bin/
├── other.rs
└── another.rs // 每个 bin/ 文件独立二进制
// lib.rs — 声明模块
pub mod front_of_house; // 从 front_of_house.rs 或 front_of_house/mod.rs 加载
mod back_of_house; // 私有模块,仅当前 crate 可见
pub(crate) mod utils; // 对整个 crate 公开但外部不可见
// front_of_house.rs
pub mod hosting; // 从 hosting.rs 加载
// front_of_house/hosting.rs
pub fn add_to_waitlist() {}
fn seat_at_table() {} // 默认私有
pub fn public_fn() {} // 外部可见
fn private_fn() {} // 默认私有(当前模块 + 子模块)
pub(crate) fn crate_visible() {} // 整个 crate 可见
pub(super) fn parent_visible() {} // 父模块可见
pub(self) fn module_visible() {} // 当前模块可见(等价默认)
pub(in crate::foo) fn restricted() {} // 指定路径可见(nightly)
// 绝对路径 — crate:: 或 根
use crate::front_of_house::hosting;
use std::collections::HashMap;
// 相对路径 — self:: 或 super::
use self::back_of_house::Cook;
use super::parent_module::helper;
// 嵌套路径
use std::{cmp::Ordering, io};
use std::io::{self, Write};
// Glob(谨慎使用)
use std::collections::*;
// 别名
use std::fmt::Result as FmtResult;
// 重新导出(pub use)
pub use crate::front_of_house::hosting;
// 外部现在可以通过 my_crate::hosting 访问
# Cargo.toml (workspace root)
[workspace]
members = [
"crates/core",
"crates/utils",
"app",
]
resolver = "3"
my-workspace/
├── Cargo.toml # workspace 定义
├── crates/
│ ├── core/
│ │ ├── Cargo.toml # [package] + [dependencies]
│ │ └── src/lib.rs
│ └── utils/
│ ├── Cargo.toml
│ └── src/lib.rs
├── app/
│ ├── Cargo.toml
│ └── src/main.rs
└── Cargo.lock # 共享 lock 文件
# crates/utils/Cargo.toml — 引用 workspace 内 crate
[dependencies]
core = { path = "../core" }
#[cfg(target_os = "linux")]
fn only_linux() {}
#[cfg(not(target_os = "windows"))]
fn not_windows() {}
#[cfg(feature = "serde")]
fn with_serde() {}
// cfg! 宏(运行时检查)
if cfg!(target_os = "linux") {
println!("Running on Linux");
}
// cfg_attr
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct Config;
cargo new my-app # 二进制项目
cargo new my-lib --lib # 库项目
cargo init # 当前目录初始化
cargo new --vcs none # 不初始化 git
# 使用模板(需要 cargo-generate)
cargo install cargo-generate
cargo generate --git https://github.com/rust-unofficial/patterns.git
Step 1. 确认项目类型 — 单 crate 包、多 crate 包还是工作空间 Step 2. 选择命名约定 — 按项目用途确定二进制和库名称(snake_case) Step 3. 规划模块层次 — 从 lib.rs 开始按功能域拆分模块文件和目录 Step 4. 设计可见性接口 — 确定哪些类型/函数是 pub/pub(crate) 还是私有 Step 5. 组织路径与 use — 配置 use 导入,确保不违反模块可见性规则 Step 6. 验证 — cargo check 确认编译通过,检查 IDE 模块导航正常