文章目录
- 使用cargo组织crate
- 重导出
- 编译
- 文档生成
- 测试
- cargo组织工作空间 TODO
- crate.io账号 TODO暂时不看
- 发布crate
使用cargo组织crate
重导出
在模块顶部使用pub use self::
重导出,方便使用模块时候直接使用use mod_X::xxx
。从而隐藏crate内部模块的结构。方便向外部暴露接口。
mod_x.rs
如下:
pub use self::mod_A:fun_01;
pub use self::mod_B:type_01;
编译
cargo build
cargo build relesase
分别对应cargo.toml
中的编译选项优化配置
# 编译选项
[profile.dev] # cargo build 默认使用 opt-level = 0 ,可选自行配置替换默认配置
opt-level = 0 # 优化基本从0-3可选
[profile.release] # cargo build --relsase 默认使用 opt-level = 0
opt-level = 3
文档生成
//!
:注释文本,为模块或 crate 提供顶层文档,写在模块或 crate 的顶层。能够支持markdown格式。///
:注释文档,为具体的项(如函数、结构体)提供文档,写在函数、结构体定义前。能够支持markdown格式。//
:普通注释,不生成文档。
//! # My Crate
//!
//! `my_crate` is a collection of utilities to make performing certain
//! calculations more convenient.
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
cargo doc --open #生成当前crate的文档
测试
cargo test
- 测试会执行注释中Examples下的示例代码
cargo组织工作空间 TODO
crate.io账号 TODO暂时不看
发布crate
[package]
name = "guessing_game" # 包名需要全网唯一
version = "0.1.0"
edition = "2021"
description = "A fun game where you guess what number the computer has chosen." # 描述信息
license = "MIT OR Apache-2.0" # 版权信息
[dependencies]