发布rust crate和cargo工作空间
文章目录
- 一、cargo构建的配置类型:dev与release两种
- 1.编译级别
- 2.将 crate 发布到 Crates.io
- 对整个库的注释
- pub use再导出功能
- 发布crates.io
- 参考
一、cargo构建的配置类型:dev与release两种
$ cargo buildFinished dev [unoptimized + debuginfo] target(s) in 0.0 secs
$ cargo build --releaseFinished release [optimized] target(s) in 0.0 secs
dev一般用户本地测试,release用于发布
1.编译级别
Cargo.toml
[profile.dev]
opt-level = 0[profile.release]
opt-level = 3
opt-level 设置控制 Rust 会对代码进行何种程度的优化。这个配置的值从 0 到 3。越高的优化级别需要更多的时间编译,所以如果你在进行开发并经常编译,可能会希望在牺牲一些代码性能的情况下编译得快一些。这就是为什么 dev 的 opt-level 默认为 0。
2.将 crate 发布到 Crates.io
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
/*** 文档注释以///开始,并使用Markdown格式(可以格式化为html);文档中的test case可以直接执行* 普通注释使用//开始*/
pub fn add_one(x: i32) -> i32 {x + 1
}
生成文档
cargo doc --openDocumenting my_crate v0.1.0 (/home/wangji/installer/rust/bobo/my_crate)Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.07sOpening /home/wangji/installer/rust/bobo/my_crate/target/doc/my_crate/index.html
执行测试
- 会执行文档中的test case
cargo testFinished `test` profile [unoptimized + debuginfo] target(s) in 0.00sRunning unittests src/lib.rs (target/debug/deps/my_crate-de08d9d1f1e709c2)running 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sRunning unittests src/main.rs (target/debug/deps/my_crate-65b3f3bb46d92877)running 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sDoc-tests my_craterunning 1 test
test src/lib.rs - add_one (line 5) ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.12s
对整个库的注释
//! # 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);
/// ```
/*** 文档注释以///开始,并使用Markdown格式(可以格式化为html);文档中的test case可以直接执行* 普通注释使用//开始*/
pub fn add_one(x: i32) -> i32 {x + 1
}
pub use再导出功能
lib.rs
//! # Art
//!
//! A library for modeling artistic concepts.
//!
//!// pub use 再导出功能
pub use self::kinds::PrimaryColor;
pub use self::kinds::SecondaryColor;
pub use self::utils::mix;pub mod kinds {/// The primary colors according to the RYB color model.pub enum PrimaryColor {Red,Yellow,Blue,}/// The secondary colors according to the RYB color model.pub enum SecondaryColor {Orange,Green,Purple,}
}pub mod utils {use crate::kinds::*;/// Combines two primary colors in equal amounts to create/// a secondary color.pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {// --snip--SecondaryColor::Orange}
}
main.rs
// 通过全路径访问这些模块
use my_crate::kinds::PrimaryColor;
use my_crate::utils::mix;// pub use 访问方式简化,上面和下面选择一个就行
use my_crate::mix;
use my_crate::PrimaryColor;fn main() {let red = PrimaryColor::Red;let yellow = PrimaryColor::Yellow;mix(red, yellow);
}
发布crates.io
首先登录
创建一个token
使用cargo login进行登录
cargo login
cargo publish
参考
- 第14章~发布一个rust crate