> 技术文档 > rust-包和箱子

rust-包和箱子


📦 图解 Rust 代码组织层级

#mermaid-svg-fBDy1PDZZ6bi000z {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .error-icon{fill:#552222;}#mermaid-svg-fBDy1PDZZ6bi000z .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fBDy1PDZZ6bi000z .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-fBDy1PDZZ6bi000z .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fBDy1PDZZ6bi000z .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fBDy1PDZZ6bi000z .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fBDy1PDZZ6bi000z .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fBDy1PDZZ6bi000z .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fBDy1PDZZ6bi000z .marker.cross{stroke:#333333;}#mermaid-svg-fBDy1PDZZ6bi000z svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fBDy1PDZZ6bi000z .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .cluster-label text{fill:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .cluster-label span{color:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .label text,#mermaid-svg-fBDy1PDZZ6bi000z span{fill:#333;color:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .node rect,#mermaid-svg-fBDy1PDZZ6bi000z .node circle,#mermaid-svg-fBDy1PDZZ6bi000z .node ellipse,#mermaid-svg-fBDy1PDZZ6bi000z .node polygon,#mermaid-svg-fBDy1PDZZ6bi000z .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fBDy1PDZZ6bi000z .node .label{text-align:center;}#mermaid-svg-fBDy1PDZZ6bi000z .node.clickable{cursor:pointer;}#mermaid-svg-fBDy1PDZZ6bi000z .arrowheadPath{fill:#333333;}#mermaid-svg-fBDy1PDZZ6bi000z .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fBDy1PDZZ6bi000z .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fBDy1PDZZ6bi000z .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-fBDy1PDZZ6bi000z .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-fBDy1PDZZ6bi000z .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fBDy1PDZZ6bi000z .cluster text{fill:#333;}#mermaid-svg-fBDy1PDZZ6bi000z .cluster span{color:#333;}#mermaid-svg-fBDy1PDZZ6bi000z div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-fBDy1PDZZ6bi000z :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}包含分为可以包含PackageCrate二进制 Crate库 Cratemain.rslib.rs多个二进制 Cratesrc/bin/*.rs


1️⃣ 箱子(Crate)——最小的编译单元

  • 类型

    • 🎁 二进制箱子:可执行程序(如命令行工具),必须有 main() 函数。
    • 📚 库箱子:共享功能(如 rand 库),没有 main()
      • 示例:src/lib.rs
  • 特点

    • 编译器每次处理 一个箱子(即使你直接编译单个文件)。
    • 箱子根(Crate Root)是编译器开始读取的入口文件(如 main.rslib.rs)。

2️⃣ 包(Package)——管理箱子的容器

  • 规则

    • 一个包 必须包含至少一个箱子(二进制或库)。
    • 一个包 最多只能有一个库箱子
    • 一个包可以有 多个二进制箱子(通过 src/bin/*.rs 添加)。
  • 示例

    my-project/├── Cargo.toml # 包的配置文件└── src/ ├── main.rs # 二进制箱子(默认与包同名) └── lib.rs # 库箱子(如果存在) └── bin/ # 额外二进制箱子 ├── tool1.rs └── tool2.rs

🛠️ 创建包时的自动结构

当你运行 cargo new

  • 二进制包

    cargo new my-app

    生成:

    my-app/├── Cargo.toml└── src/ └── main.rs # 二进制箱子入口
  • 库包

    cargo new my-lib --lib

    生成:

    my-lib/├── Cargo.toml└── src/ └── lib.rs # 库箱子入口

🌰 实际案例对比

概念 类比 示例 箱子 单个“代码盒子” main.rslib.rs 装多个盒子的“快递” 整个项目目录

常见疑问

  • Q:为什么要有箱子和包的分层?
    A:箱子是编译的最小单元(Rust 的编译优化以箱子为单位),包是方便人类管理的逻辑单元(通过 Cargo.toml 统一配置)。

  • Q:如何共享代码?
    A:将公共代码放在 lib.rs 中,二进制箱子通过 use my-lib; 调用。