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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值