乐趣区

关于rust:rust学习将模块分成不同的文件

随着程序的越来越简单,咱们须要将单文件宰割成多文件。
当初创立 main.rs 文件,其内容如下:

mod front_of_house;

pub use crate::front_of_house::hosting;

pub fn eat_at_restaurant() {hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();}

fn main(){eat_at_restaurant()
}

创立 front_of_house.rs 文件,其内容如下:

pub mod hosting;

创立文件夹 src/front_of_house 并新增加一个文件 hosting.rs,其内容如下:

#![allow(unused_variables)]
pub fn add_to_waitlist() {println!("被调用了!!!")
}

运行一下:

D:\learn\cargo_learn>cargo run
   Compiling cargo_learn v0.1.0 (D:\learn\cargo_learn)
    Finished dev [unoptimized + debuginfo] target(s) in 0.47s
     Running `target\debug\cargo_learn.exe`
被调用了!!!被调用了!!!被调用了!!!D:\learn\cargo_learn>

留神:若想调用一个内部文件裸露进去的模块并将其裸露进来,那么就必须新建一个跟本文件同名称的文件夹,并将内部文件放入此文件夹中,最好模块拆分的很洁净能和文件名称保持一致最好

退出移动版