发布rust crate
文章目录
- 一、cargo构建的配置类型:dev与release两种
- 1.编译级别
- 2.将 crate 发布到 Crates.io
- 对整个库的注释
- pub use再导出功能
- 发布crates.io
- 参考
一、cargo构建的配置类型:dev与release两种
$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
$ cargo build --release
Finished 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 --open
Documenting my_crate v0.1.0 (/home/wangji/installer/rust/bobo/my_crate)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.07s
Opening /home/wangji/installer/rust/bobo/my_crate/target/doc/my_crate/index.html
执行测试
- 会执行文档中的test case
cargo test
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.00s
Running unittests src/lib.rs (target/debug/deps/my_crate-de08d9d1f1e709c2)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/debug/deps/my_crate-65b3f3bb46d92877)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests my_crate
running 1 test
test src/lib.rs - add_one (line 5) ... ok
test 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.toml文件中增加license文件说明
git commit代码
cargo publish
上传到crate.io中crate的只能更新或者禁止他人使用
禁止他人使用的方法如下:
cargo yank --vers 0.1.0
取消他人使用
cargo yank --vers 0.1.0 undo
参考
- 第14章~发布一个rust crate