一、rust代码组织
mod front_of_house {
mod hosting {
fn add_to_waitlist(){}
fn seat_at_table(){}
}
mod serving {
fn take_order(){}
fn serve_order(){}
fn take_payment(){}
}
}
二、路径
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub fn eat_at_restaurant() {
// 绝对路径调用方法
//crate::front_of_house::hosting:add_to_waitlist();
// 相对路径调用方法
front_of_house::hosting::add_to_waitlist();
}
fn serve_order(){}
mod back_of_house {
fn fix_wrong_order() {
cook_order();
// 相对路径调用
super::serve_order();
// 绝对路径调用
crate::serve_order();
}
fn cook_order(){}
}
需要把seasonal_fruit变成pub 公共的,就不会报错了
都是公共的,才有作用
三、Use关键字
改成pub后,下面才能调用mod中的pub函数
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist(){}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
use rand::Rng;
use std::collections::HashMap;
//use std::collections::HashMap;
//use std::cmp::Ordering;
use std::{cmp::Ordering,io}; // 上面两行缩成一行