Corresponding explanation contents of rust programming video tutorial – package, crite and module

Time:2021-10-16

Video address

Headline address:https://www.ixigua.com/i676544267458235648…
Station B address:https://www.bilibili.com/video/av78062009?…
Netease cloud classroom address:https://study.163.com/course/introduction….

Explanation content

1. Definition
(1) Package: a feature of cargo that allows you to build, test, and share crates.
(2) Crite: the tree structure of a module, forming a library or binary project.
(3) Module: used through use to control the privacy of scope and path.
(4) Path: a way to name items such as structures, functions, or modules.

2. Package and crate
(1)crate root   Is a source file. The rust compiler takes it as the starting point and constitutes the root module of your crite.
(2) The package provides one or more Crites of a series of functions.
(3) Crite root is Src / main.rs or Src / lib.rs. Note: if there is only main.rs, it means that the package has only one crate (main). If it has both main.rs and other lib.rs (not necessarily this name), it means that it has multiple crates.
(4) Crite will group the related functions of a scope together, so that the function can be easily shared among multiple projects.

3. Use modules to control scope and privacy
(1) Create a lib by using the command cargo new — lib libname.
(2) By default, all items (functions, methods, structs, enumerations, modules, and constants) in are private and need to use pub to be exposed to the outside.
(3) Create a module, for example:

//factory.rs
Mod refrigerator {// refrigerator // you need to use pub, otherwise others can't use it
    FN refrigeration() {// you need to use pub, otherwise others can't use it
    }
}

mod washing_ Machine {// you need to use pub, otherwise others cannot use it
    FN wash() {// you need to use pub, otherwise others cannot use it
    }
}
//lib.rs
pub mod factory;
//main.rs
use mylib::factory;
fn main() {
    factory::refrigerator::refrigeration();
    println!("Hello, world!");
}

Knowledge point: you need to use pub, otherwise you can’t use it.

4. Use absolute and relative paths
example:

mod A {
    pub mod B {
        pub fn prin() {
            println!("++++++");
            super::ppp();// Use parent path
        }
    }

    pub fn ppp() {
        println!("++++++ ppp");
    }
}
fn main() {
    use A::B::prin; // Relative path
    prin();
    A::B::prin();// Absolute path
    println!("Hello, world!");
}

5. Use of structures

mod modA {
    #[derive(Debug)]
    pub struct A {
        pub number: i32,
        name: String,
    }

    impl A {
        pub fn new_a() -> A {
            A {
                number: 20,
                name: String::from("A"),
                }
        }

        pub fn print_a(&self) {
            println!("number = {}, name = {}", self.number, self.name);
        }
    }
}

fn main() {
    let a = modA::A::new_a();
    a.print_a();
    let n = a.name;// Error, because name is private
    println!("Hello, world!");
}

6. Use use and as, use * to introduce all

//Use
use modA::A;
let a = A::new_a();

//Use as
use modA::A as myA;
let a = myA::new_a();

//Use*
use modA::*;
let a = myA::new_a();

7. Re export using pub use

8. External package
Under dependencies under cargo.toml
(1) Path

[dependencies]
mylib = {path = "./mylib"}

(2) Version on GitHub

[dependencies]
rust-crypto = "0.2"
serde = "1.0.63"
bincode = "1.2.0"
serde_derive = "1.0.27"

example:

//!  Sha3-256 example
extern crate crypto;
//extern crate rustc_hex;
use self::crypto::digest::Digest;
use self::crypto::sha3::Sha3;
//use rustc_hex::{ToHex,FromHex};
fn main() {
    // create a SHA3-256 object
    let mut hasher = Sha3::sha3_256();

    // write input message
    hasher.input_str("hello world");

    //read hash digest
    let hex = hasher.result_str();
}

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed