Rust目录结构 简单使用

注意
本文最后更新于 2024-02-24,文中内容可能已过时。

Rust mod 的引用

#Rust/目录结构

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

bash

npm install -g tree-node-cli
treee -L 10 -I "node_modules|.idea|objects|.git" -a --dirs-first

text

tttttttttt  //项目名
├── src
│   ├── school
│   │   ├── a_school
│   │   │   ├── student.rs
│   │   │   └── teacher.rs
│   │   ├── mod.rs
│   │   └── sum.rs
│   └── main.rs
├── Cargo.lock
└── Cargo.toml

src\school\mod.rs

rust

pub mod a_school {
 pub mod teacher;
 pub mod student;
}
pub mod sum;

src\school\sum.rs

rust

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

src\school\a_school\student.rs

rust

pub fn num() -> i32 { 10 }

src\school\a_school\teacher.rs

rust

pub fn num() -> i32 { 50 }

src\main.rs

rust

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());
}

结果

text

> 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