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


Video address

Headline address:…
Station B address:…
Netease cloud classroom address:….

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 / or Src / Note: if there is only, it means that the package has only one crate (main). If it has both and other (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:

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
pub mod factory;
use mylib::factory;
fn main() {
    println!("Hello, world!");

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

4. Use absolute and relative paths

mod A {
    pub mod B {
        pub fn prin() {
            super::ppp();// Use parent path

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

5. Use of structures

mod modA {
    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,;

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

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

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

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

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

7. Re export using pub use

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

mylib = {path = "./mylib"}

(2) Version on GitHub

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


//!  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