Rust7.2 More About Cargo and Crates.io
Rust学习笔记
Rust编程语言入门教程课程笔记
参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)
Lecture 14: More About Cargo and Crates.io
main.rs
//Customizing Builds with Release Profiles
// cargo profile:
// 1. dev profile: cargo build
// 2. release profile: cargo build --release
// customizing release profile
// cargo.toml: profile.release or profile.dev
// Filename: Cargo.toml
// [profile.dev]
// opt-level = 0 // no optimization
// [profile.release]
// opt-level = 3 // more optimization, slower compile time
// use cargo_and_crateio::kinds::PrimaryColor;
// use cargo_and_crateio::utils::mix;
use cargo_and_crateio::PrimaryColor;
use cargo_and_crateio::mix;
fn main() {
let red = PrimaryColor::Red;
let yellow = PrimaryColor::Yellow;
mix(red, yellow);
}
lib.rs
//Publishing a Crate to Crates.io
//Making Useful Documentation Comments
//Commenting Contained Items
//! # cargo_and_crateio
//!
//! `cargo_and_crateio` is a collection of utilities to make performing certain
//! calculations more convenient.
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = cargo_and_crateio::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
//cargo doc
//run rustdoc tool to generate HTML documentation from the comments. (in the target/doc directory)
//cargo doc --open
//open documentation in browser
//commonly used sections:
// 1. Examples
// 2. Panics
// 3. Errors
// 4. Safety
//Documentation Comments as Tests
//cargo test
//Exporting a Convenient Public API with pub use
//Re-exporting Names with pub use
// //! # 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 {
// --snip--
SecondaryColor::Orange
}
}
workplaces
add/Cargo.toml
[workspace]
members = ["adder", "add_one", "add_two"]
add/target/debug: the compliled files
add/adder
Cargo.toml
[package]
name = "adder"
version = "0.1.0"
edition = "2021"
[dependencies]
add_one = {path = "../add_one"}
add/adder/src/main.rs
use add_one;
fn main() {
let num = 10;
println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num));
}
add/add_one/src/lib.rs
pub fn add(left: usize, right: usize) -> usize {
left + right
}
pub fn add_one(num: usize) -> usize {
num + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn it_adds_one() {
let result = add_one(2);
assert_eq!(result, 3);
}
}
add/add_two/src/lib.rs
pub fn add(left: usize, right: usize) -> usize {
left + right
}
pub fn add_two(num: usize) -> usize {
num + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn it_adds_two() {
let result = add_two(2);
assert_eq!(result, 4);
}
}
test the whole workplaces: run “cargo test” in the add dictionary