当前位置: 首页 > article >正文

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


http://www.kler.cn/a/137388.html

相关文章:

  • vue+element实现多级表头加树结构
  • ubuntu 解决pip/pip3安装库各种报错问题
  • 增速大幅下滑?基础L2博弈成本
  • echarts的使用
  • Go——二、变量和数据类型
  • 爬虫的http和https基础
  • 如何在公网环境下使用笔记本的Potplayer访问本地群晖webdav中的影视资源
  • 蓝桥杯物联网_STM32L071_1_CubMxkeil5基础配置
  • Docker部署FLASK Unicorn并配置Nginx
  • 【Django-02】 Model模型和模型描述对象Meta
  • 类型体系与基本数据类型(题目)
  • WinEdt 11.1编辑器的尝鲜体验
  • vscode c++ 报错identifier “string“ is undefined
  • 海外IP代理科普——API代理是什么?怎么用?
  • 一个快递包裹的跨国之旅
  • Flutter 使用 device_info_plus 遇到的问题
  • Python基础学习019--跳过
  • Django——模型层补充
  • ES Kibana 安装
  • 开发手账(一)