• 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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    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
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    workplaces

    add/Cargo.toml

    [workspace]
    members = ["adder", "add_one", "add_two"]
    
    • 1
    • 2

    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"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    test the whole workplaces: run “cargo test” in the add dictionary

  • 相关阅读:
    typescript核心
    GoLong的学习之路(六)语法之指针
    Java:Java vs .Net vs Python,选哪个好?
    微服务06-Dockerfile自定义镜像+DockerCompose部署多个镜像
    MATLAB | 三个趣的圆相关的数理性质可视化
    vue3 element plus表格导出为excel自定义表头
    数据结构之手写HashMap
    【计算机网络】第一章——计算机概述(上篇)
    PTA 7-169 斐波那契数列
    标准I/O库
  • 原文地址:https://blog.csdn.net/weixin_45347752/article/details/134466404