Rust编程语言之Cargo、Crates.io详解内容通过releaseprofile来自定义构建在https://crates.io/上发布库通过workspaces组织大工程从https://crates.io/来安装库使用自定义命令扩展cargo一、通过re
[package]
name = "closure"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[profile.dev]
opt-level = 1
[profile.release]
opt-level = 3
执行
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1 took 2.9s
➜ cargo build
Compiling closure v0.1.0 (/Users/qiaopengjun/rust/closure)
Finished dev [optimized + debuginfo] target(s) in 0.16s
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜
/// 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
}
运行
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo doc
Documenting closure v0.1.0 (/Users/qiaopengjun/rust/closure)
Finished dev [optimized + debuginfo] target(s) in 0.39s
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo doc --open
Finished dev [optimized + debuginfo] target(s) in 0.00s
Opening /Users/qiaopengjun/rust/closure/target/doc/closure/index.html
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo doc --open
Documenting closure v0.1.0 (/Users/qiaopengjun/rust/closure)
Finished dev [optimized + debuginfo] target(s) in 0.41s
Opening /Users/qiaopengjun/rust/closure/target/doc/closure/index.html
closure on master [?] is 📦 0.1.0 via 🦀 1.67.1
# Examples
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = closure::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
//! # Closure Crate
//!
//! `closure` is a collection of utilities to make performance
//! calculations more convenient.
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = closure::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
my_crate::some_module::another_module::UsefulType;
my_crate::UsefulType;
//! # Art
//!
//! A library for modeling artistic concepts.
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 {
SecondaryColor::Green
}
}
src/main.rs 文件
use art::kinds::PrimaryColor;
use art::utils::mix;
fn main() {
let red = PrimaryColor::Red;
let yellow = PrimaryColor::Yellow;
mix(red, yellow);
}
优化之后:
src/lib.rs 文件
//! # Art
//!
//! A library for modeling artistic concepts.
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 {
SecondaryColor::Green
}
}
src/main.rs 文件
// use art::kinds::PrimaryColor;
// use art::utils::mix;
use art::PrimaryColor;
use art::mix;
fn main() {
let red = PrimaryColor::Red;
let yellow = PrimaryColor::Yellow;
mix(red, yellow);
}
rust_tutorials on master [?] via 🦀 1.67.1
➜ cargo login --registry crates-io
please paste the token found on https://crates.io/me below
ciopLk54SDAxB200gA4jk85abcdefgabcabc # token
Login token for `crates-io` saved
rust_tutorials on master [?] via 🦀 1.67.1 took 1m 27.6s
➜
rust_tutorials on master [?] via 🦀 1.67.1 took 2.0s
➜ cargo publish --registry crates-io --allow-dirty
Updating crates.io index
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!