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
~/rust
➜ mcd add # mkdir add cd add
~/rust/add
➜ touch Cargo.toml
~/rust/add via 🦀 1.67.1
➜ c # code .
~/rust/add via 🦀 1.67.1
➜
~/rust/add via 🦀 1.67.1
➜ cargo new adder
warning: compiling this new package may not work due to invalid workspace configuration
current package believes it's in a workspace when it's not:
current: /Users/qiaopengjun/rust/add/adder/Cargo.toml
workspace: /Users/qiaopengjun/rust/add/Cargo.toml
this may be fixable by adding `adder` to the `workspace.members` array of the manifest located at: /Users/qiaopengjun/rust/add/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
Created binary (application) `adder` package
~/rust/add via 🦀 1.67.1
➜ cargo build
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/add/adder)
Finished dev [unoptimized + debuginfo] target(s) in 0.48s
~/rust/add via 🦀 1.67.1
~/rust/add via 🦀 1.67.1
➜ cargo new add-one --lib
Created library `add-one` package
~/rust/add via 🦀 1.67.1
➜
add-one/src/lib.rs 文件
pub fn add_one(x: i32) -> i32 {
x + 1
}
adder/src/main.rs 文件
use add_one;
fn main() {
let num = 10;
println!("Hello, world! {} plus one is {}", num, add_one::add_one(num));
}
adder/Cargo.toml 文件
[package]
name = "adder"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
add-one = { path = "../add-one" }
rust/add/Cargo.toml 文件
[workspace]
members = [
"adder",
"add-one",
]
运行
~/rust/add via 🦀 1.67.1
➜ cargo build
Compiling add-one v0.1.0 (/Users/qiaopengjun/rust/add/add-one)
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/add/adder)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
~/rust/add via 🦀 1.67.1
➜ cargo run -p adder
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/adder`
Hello, world! 10 plus one is 11
~/rust/add via 🦀 1.67.1
➜
rust/add/add-one/src/lib.rs 文件
pub fn add_one(x: i32) -> i32 {
x + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(3, add_one(2));
}
}
执行测试
~/rust/add via 🦀 1.67.1
➜ cargo test
Compiling add-one v0.1.0 (/Users/qiaopengjun/rust/add/add-one)
Compiling adder v0.1.0 (/Users/qiaopengjun/rust/add/adder)
Finished test [unoptimized + debuginfo] target(s) in 0.13s
Running unittests src/lib.rs (target/debug/deps/add_one-cb079acb8d173784)
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/debug/deps/adder-23a2e001f7410351)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests add-one
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
~/rust/add via 🦀 1.67.1
➜
~/rust/add via 🦀 1.67.1
➜ cargo test -p add-one
Finished test [unoptimized + debuginfo] target(s) in 0.00s
Running unittests src/lib.rs (target/debug/deps/add_one-cb079acb8d173784)
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests add-one
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
~/rust/add via 🦀 1.67.1
➜
~/rust took 2m 8.1s
➜ cargo install rust_tutorials_qiao
Updating `tuna` index
Downloaded rust_tutorials_qiao v0.1.0 (registry `tuna`)
Downloaded 1 crate (759 B) in 3.43s
Installing rust_tutorials_qiao v0.1.0
Compiling rust_tutorials_qiao v0.1.0
Finished release [optimized] target(s) in 3.99s
Installing /Users/qiaopengjun/.cargo/bin/rust_tutorials_qiao
Installed package `rust_tutorials_qiao v0.1.0` (executable `rust_tutorials_qiao`)
~/rust took 4.0s
➜
➜ rust_tutorials_qiao
Hello, world!
~/rust took 3.1s
➜
~/rust took 3.1s
➜ echo $PATH # 查看PATH环境变量
cargo --list
➜ cargo --list
Installed Commands:
add Add dependencies to a Cargo.toml manifest file
b alias: build
bench Execute all benchmarks of a local package
build Compile a local package and all of its dependencies
c alias: check
check Check a local package and all of its dependencies for errors
clean Remove artifacts that cargo has generated in the past
clippy Checks a package to catch common mistakes and improve your Rust code.
config Inspect configuration values
d alias: doc
doc Build a package's documentation
fetch Fetch dependencies of a package from the network
fix Automatically fix lint warnings reported by rustc
fmt Formats all bin and lib files of the current crate using rustfmt.
generate-lockfile Generate the lockfile for a package
git-checkout This command has been removed
help Displays help for a cargo subcommand
init Create a new cargo package in an existing directory
install Install a Rust binary. Default location is $HOME/.cargo/bin
locate-project Print a JSON representation of a Cargo.toml file's location
login Save an api token from the registry locally. If token is not specified, it will be read from stdin.
logout Remove an API token from the registry locally
metadata Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format
miri
new Create a new cargo package at <path>
owner Manage the owners of a crate on the registry
package Assemble the local package into a distributable tarball
pkgid Print a fully qualified package specification
publish Upload a package to the registry
r alias: run
read-manifest Print a JSON representation of a Cargo.toml manifest.
remove Remove dependencies from a Cargo.toml manifest file
report Generate and display various kinds of reports
rm alias: remove
run Run a binary or example of the local package
rustc Compile a package, and pass extra options to the compiler
rustdoc Build a package's documentation, using specified custom flags.
search Search packages in crates.io
t alias: test
test Execute all unit and integration tests and build examples of a local package
tree Display a tree visualization of a dependency graph
uninstall Remove a Rust binary
update Update dependencies as recorded in the local lock file
vendor Vendor all dependencies for a project locally
verify-project Check correctness of crate manifest
version Show version information
yank Remove a pushed crate from the index
~/rust
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!