【笔记】Rust的模块

Introduction

Notes on learning modules in Rust.
In Rust, you can create modules with a main function or modules without a main function.

Creating a Library Module in a Project

  • Create a library module without a main function in a project.

<project_name>: project name
<module_name>: module name

1
2
cd <project_name>
cargo new --lib <module_name>
  • Directory structure after creation
1
2
3
4
5
6
7
8
9
+ <project_name>
+ <module_name>
+ src
- lib.rs
- Cargo.toml
+ src
- main.rs
- Cargo.lock
- Cargo.toml

Defining a Private Module

  • By default, modules defined in Rust are private.
  • Private modules cannot be referenced by other external modules.
  • All functions in private modules are also private.
1
2
3
4
5
mod module_name {
fn function_name() {
...
}
}

Defining a Public Module

  • Modules decorated with the pub keyword are public and can be referenced by other external modules.
  • Public modules can define both private and public functions.
1
2
3
4
5
pub mod module_name {
fn function_name() {
...
}
}

Importing a Module

  • Public modules can be imported at the top of a file, allowing access to the functions defined within.
1
2
3
use public_module::function_name;

function_name();

Done

References

Bilibili - Learning for Salary Increase
title: 【笔记】Rust的模块
categories:
- Rust学习指北
tags:
- Rust
sticky: 0
lang: en
date: 2023-09-18 18:13:36

Introduction

Notes on module learning in Rust.
In Rust, you can create modules with a main function, or modules without a main function.

Creating a Library Module in a Project

  • Create a library module without a main function in the project.

<project_name>: project name
<module_name>: module name

1
2
cd <project_name>
cargo new --lib <module_name>
  • Directory structure after creation
1
2
3
4
5
6
7
8
9
+ <project_name>
+ <module_name>
+ src
- lib.rs
- Cargo.toml
+ src
- main.rs
- Cargo.lock
- Cargo.toml

Defining Modules

Defining a Private Module

  • Modules defined in Rust are private by default.
  • Private modules cannot be referenced by other external modules.
  • All functions in a private module are private.
//src/lib.rs
1
2
3
4
5
mod module_name {
fn function_name() {
...
}
}

Defining a Public Module

  • Modules decorated with the pub keyword are public modules, which can be referenced by other external modules.
  • Public modules can define both private and public functions.
//src/lib.rs
1
2
3
4
5
pub mod module_name {
fn function_name() {
...
}
}

Defining Nested Modules

  • Modules can be nested within modules.
//src/lib.rs
1
2
3
4
5
6
7
pub mod module_name {
pub mod module_name {
fn function_name() {
...
}
}
}

Importing Modules into a Project

Importing a Module from the Current Project

/Cargo.toml
1
2
[dependencies]
module_name = { path = "../project_name/module_name" }

Calling Functions in a Module

  • Public modules can be referenced in the file header. After importing, functions defined within the public module can be used.
//src/lib.rs
1
2
3
use module_name::function_name;

function_name();

Using Functions from a Nested Module

//src/lib.rs
1
2
3
use module_name::module_name::function_name;

function_name();

Done

References

Bilibili - Learning for a Raise