"首次提交"

This commit is contained in:
2024-06-08 15:01:12 +08:00
parent 6e0f708d0a
commit 84349a2469
112 changed files with 3272 additions and 0 deletions

1
module_learn/.gitignore vendored Executable file
View File

@ -0,0 +1 @@
/target

8
module_learn/Cargo.toml Executable file
View File

@ -0,0 +1,8 @@
[package]
name = "module_learn"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

7
module_learn/src/bin/app1.rs Executable file
View File

@ -0,0 +1,7 @@
/*
rust 可以有多个二进制的crate目录都是在src/bin 目录下。每个rs就是一个二进制包文件
比如这里就是二进制crate app1使用cargo build --bin app1 即可进行编译
*/
fn main() {
println!("Hello, world! app1");
}

3
module_learn/src/bin/app2.rs Executable file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world! app2");
}

132
module_learn/src/lib.rs Executable file
View File

@ -0,0 +1,132 @@
/*
rust 只可以有一个library的单元包文件的路径必须是src/lib.rs。包名就是cargo new的时候创建的工程名。
可以使用 cargo build --lib 进行编译
*/
//该模块位public的可以被外部的code进行导入使用
pub mod level0_mod1{
//定义一个public的函数可以暴露给外部调用
pub fn func(){
println!("This is level0_mod1::func");
//fun1属于本模块内的私有函数。模块内的code 是可以使用的,
fun1();
levl1_mod1::func();
//levl1_mod1::fun1是私有的只能在levl1_mod1 子模块内使用。
//levl1_mod1::fun1();
//levl2_mod1 是私有的只能在 levl1_mod2 模块内使用。不能被更外部的code使用
//levl1_mod2::levl2_mod1::func();
}
//定义一个私有的函数。外部不能直接使用只能模块内部的code可以使用
fn fun1() {
println!("This is level0_mod1::func1");
levl1_mod1::func();
}
//定义一个私有的模块。只能被同级的其他模块及其子模块调用不能被外部的code调用
mod levl1_mod1 {
pub fn func(){
println!("This is levl1_mod1::func");
self::fun1();
}
//这一一个私有模块中的私有函数。只能被同模块下的其他code调用不能被本模块外的code调用
fn fun1() {
println!("This is levl1_mod1::func1");
}
}
//定义一个公共的子模块。由于其父模块也是public的所以其可以被外部的code进行调用
pub mod levl1_mod2 {
pub fn func() {
println!("This is levl1_mod2::func");
levl2_mod1::func();
}
mod levl2_mod1 {
pub fn func(){
println!("This is levl2_mod1::func");
//可以使用绝对路径调用函数。根目录就是creat关键字相对于文件系统目录里面/
crate::level0_mod1::levl1_mod1::func();
//也可以使用相对路径进行调用
//self 相当于文件系统里面的 . 代表当前目录,也就是当前模块
//super 相当于文件系统里面的 .. 代表当前目录里的上一级目录,也就是父级模块
self::super::super::levl1_mod1::func();
//这个函数是 levl1_mod1 中私有的不能跨mod使用
//crate::level0_mod1::levl1_mod1::fun1();
//这样也是ok的表示同级模块下的其他函数
self::func1();
}
fn func1(){
println!("This is levl2_mod1::func1");
}
}
}
}
/*
模块和里面的函数数据等默认都是私有的如果要暴露给外部使用则需要加上pub关键字
如果是模块私有的,则只能被同模块或者子模块进行调用,不能被外部调用
如果函数是私有的那么只能被同模块下的code进行调用。即使其所属模块是公共的其他和所述模块同级的模块也不能使用该函数
总之,如果是私有的就是不能被跨模块被使用。但是对于所有的本模块内的成员来说都是公共的。
所以:
如果我们定义一个模块像提供一些功能给外部使用那么可以把这些API都定义到一个公共的mod中
其他具体的实现可以放在另外的私有模块中然后在这个私有模块里将其具体的实现设置为pub的这样就可以只让暴露到外部的API的所述模块
调用到这里具体的实现而进行实作,但是外部无法使用。
*/
//本模块的函数
pub fn func(){
println!("This is level0_mod1::func");
}
//定义一个结构体并加上pub关键字使其暴露给外部使用。
pub struct struct1{
/* 即使结构体是pub的但是不加pub的成员依然是私有的 */
pub member1: i32, //该member是公有的外部可以访问到
member2:i32, //该member是私有的。外部无法访问到
}
impl struct1{
//公共的函数,可以被外部使用
pub fn func(){
println!("This is struct1->func");
}
//私有的函数,不可以被外部使用
fn func1(){
println!("This is struct1->func1");
}
}
//定义一个枚举并加上pub关键字使其暴露给外部使用。
//对于枚举来说 和模块以及struct不同只要枚举是pub的那么其变体也都是pub的
//因为只有只有所有变体都是pub的才能起到最大作用
pub enum enum1{
member1, //该member是公有的外部可以访问到
member2, //该member是公有的外部可以访问到
}
impl enum1 {
//公共的函数,可以被外部使用
pub fn func(){
println!("This is struct1->func");
}
//私有的函数,不可以被外部使用
fn func1(){
println!("This is struct1->func1");
}
}
/* 使用文件目录的形式拆分module以便于减小单个文件的代码量
在lib.rs 里面定义模块名在同级目录建立module同名的rs文件。为该module的实现
如果module还存在子module那就再建立和module同名的文件夹在文件夹里面建立sub_module.rs存放sub_module的实现
如此往复形成树状结构的代码源码目录。
├─src
│ ├─bin #二进制单元包, 可以有多个每个单独为一个rs文件
│ │ ├─app1.rs
│ │ └─app2.rs
│ ├─lib.rs #library 单元包 只能有一个入口必须是lib.rs
│ ├─tree0_mod1.rs #module tree0_mod1
│ └─tree0_mod1 #所有 module tree0_mod1 的子module的实现源码存放目录
│ ├─tree1_mod2.rs #module tree1_mod2 ,其为 tree0_mod1 的子module
│ └─tree1_mod2 #所有 module tree1_mod2 的子module的实现源码存放目录
│ └─tree2_mod1.rs #module tree2_mod1其为 tree1_mod2 的子module
*/
pub mod tree0_mod1;

21
module_learn/src/main.rs Executable file
View File

@ -0,0 +1,21 @@
//使用模块路径进行导入, module_learn 代表的是这个lib的名字。 level0_mod1代表是这个lib下的模块
use module_learn::level0_mod1;
//使用{}可以一次性导入多个模块/函数等,不必多行
use module_learn::level0_mod1::{levl1_mod2, func};
use module_learn;
use module_learn::tree0_mod1;
//如果导入时存在重名的可以使用as关键字起一个别名去辨别。
use std::io::Result as ioResult;
use std::fmt::Result as fmtResult;
fn main() {
println!("Hello, world!");
level0_mod1::func();
func();
levl1_mod2::func();
module_learn::func();
tree0_mod1::func();
}

23
module_learn/src/tree0_mod1.rs Executable file
View File

@ -0,0 +1,23 @@
//定义一个public的函数可以暴露给外部调用
pub fn func(){
println!("This is tree0_mod1::func");
//fun1属于本模块内的私有函数。模块内的code 是可以使用的,
fun1();
tree1_mod1::func();
//tree1_mod1::fun1是私有的只能在tree1_mod1 子模块内使用。
//tree1_mod1::fun1();
//tree2_mod1 是私有的只能在 tree1_mod2 模块内使用。不能被更外部的code使用
//tree1_mod2::tree2_mod1::func();
}
//定义一个私有的函数。外部不能直接使用只能模块内部的code可以使用
fn fun1() {
println!("This is tree0_mod1::func1");
tree1_mod1::func();
}
//定义一个私有的模块。只能被同级的其他模块及其子模块调用不能被外部的code调用
mod tree1_mod1;
//定义一个公共的子模块。由于其父模块也是public的所以其可以被外部的code进行调用
pub mod tree1_mod2;

View File

@ -0,0 +1,8 @@
pub fn func(){
println!("This is tree1_mod1::func");
self::fun1();
}
//这一一个私有模块中的私有函数。只能被同模块下的其他code调用不能被本模块外的code调用
fn fun1() {
println!("This is tree1_mod1::func1");
}

View File

@ -0,0 +1,5 @@
pub fn func() {
println!("This is tree1_mod2::func");
tree2_mod1::func();
}
mod tree2_mod1;

View File

@ -0,0 +1,16 @@
pub fn func(){
println!("This is tree2_mod1::func");
//可以使用绝对路径调用函数。根目录就是creat关键字相对于文件系统目录里面/
crate::tree0_mod1::tree1_mod1::func();
//也可以使用相对路径进行调用
//self 相当于文件系统里面的 . 代表当前目录,也就是当前模块
//super 相当于文件系统里面的 .. 代表当前目录里的上一级目录,也就是父级模块
self::super::super::tree1_mod1::func();
//这个函数是 tree1_mod1 中私有的不能跨mod使用
//crate::level0_mod1::tree1_mod1::fun1();
//这样也是ok的表示同级模块下的其他函数
self::func1();
}
fn func1(){
println!("This is tree2_mod1::func1");
}