Rust目录结构 简单使用

Rust mod 的引用

#Rust/目录结构

tree-clitree-node-cli 可以显示,本文使用 tree-node-cli

1
2
npm install -g tree-node-cli
treee -L 10 -I "node_modules|.idea|objects|.git" -a --dirs-first
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
tttttttttt  //项目名
├── src
│   ├── school
│   │   ├── a_school
│   │   │   ├── student.rs
│   │   │   └── teacher.rs
│   │   ├── mod.rs
│   │   └── sum.rs
│   └── main.rs
├── Cargo.lock
└── Cargo.toml

src\school\mod.rs

1
2
3
4
5
pub mod a_school {
 pub mod teacher;
 pub mod student;
}
pub mod sum;

src\school\sum.rs

1
2
3
4
use crate::school::a_school;
pub fn get_sum() -> i32 {
 a_school::student::num()+a_school::teacher::num()
}

src\school\a_school\student.rs

1
pub fn num() -> i32 { 10 }

src\school\a_school\teacher.rs

1
pub fn num() -> i32 { 50 }

src\main.rs

1
2
3
4
5
6
7
8
9
pub mod school;
use school::a_school::student::num as num1;
use school::a_school::teacher::num as num2;
fn main() {
 println!("Hello, world!"); 
 dbg!(num1());
 dbg!(num2());
 dbg!(school::sum::get_sum());
}

结果

1
2
3
4
5
6
7
8
> Executing task: C:\Users\icer\.cargo\bin\cargo.exe run --package tttttttttt --bin tttttttttt <
   Compiling tttttttttt v0.1.0 (D:\rustcode\tttttttttt)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target\debug\tttttttttt.exe`
Hello, world!
[src\main.rs:8] num1() = 10
[src\main.rs:9] num2() = 50
[src\main.rs:10] school::sum::get_sum() = 60