模块系统(module system):
单元包被用来生成二进制程序或库。Rust编译时的入口函数称作这个单元包的根节点和根模块。包由一或多个相关单元包集合而成,配置文件Cargo.toml描述构建信息。
包最多一个库单元包,任意多个二进制单元包,至少一个单元包(库或二进制)。
cargo new project
#配置文件
Cargo.toml
#二进制单元包(与包同名)根节点
src/main.rs
#库单元包(与包同名)根节点,手动添加
src/lib.rs
#创建更多二进制单元包,每个源文件视作单独的二进制单元包,手动添加
src/bin
#创建restaurant库
cargo new --lib restaurant
src/lib.rs
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() {}
}
}
模块树结构
crate
|_ front_of_house
|_ hosting
| |_ add_to_waitlist
| |_ seat_at_table
|_ serving
|_ take_order
|_ serve_order
|_ take_payment
模块A被包含在模块B内时,模块A称作模块B的子节点(child),模块B称作模块A的父节点(parent)。整个模块放置在隐式根模块crate下。
两种形式:
路径至少含一个标识符,标识符间::分隔。
src/lib.rs
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
}
}
pub fn eat_at_restaruant() {
//绝对路径
crate::font_of_house::hosting::add_wo_waitlist();
//相对路径
font_of_house::hosting::add_wo_waitlist();
}
Rust中的所有条目(函数、方法、结构体、枚举、模块及常量)默认都是私有的。父模块条目无法使用子模块条目,子模块条目可以使用祖先模块条目。
eat_at_restaruant与front_of_house属于同级节点,可以直接在eat_at_restaruant中引用front_of_house(font_of_house)。
hosting 模块标记为pub,可以访问(font_of_house::hosting)。
add_to_waitlist 函数标记为pub,可以访问(font_of_house::hosting::add_wo_waitlist())。
src/lib.rs
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub fn eat_at_restaruant() {
//绝对路径
crate::font_of_house::hosting::add_wo_waitlist();
//相对路径
font_of_house::hosting::add_wo_waitlist();
}
src/lib
fn server_order() {}
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
supper::serve_order();
}
fn cook_order() {}
}
结构体定义前使用pub,它的字段依旧保持私有状态。
mod back_of_house {
pub struct Breakfast{
pub toast: String,
seasonal_fruit: String,
}
impl Breakfast {
pub fn summer(toast: &str) -> Breakfast {
Breakfast {
toast: String::from(toast),
seasonal_fruit: String::ffrom("peaches"),
}
}
}
}
pub fn eat_at_restaurant() {
let mut meal = back_of_house::Breakfast::summer("Rye");
//修改toast
meal.toast = String::from("Wheat");
println!("I'd like {} toast please", meal.toast);
//出错
//.seasonal_fruit= String::from("blueberries");
}
枚举声明为pub,它的所有变体都成了公共状态,所有枚举变体默认pub。
src/lib
mod back_of_house {
pub enum Appetizer {
Soup,
Salad,
}
}
pub fn eat_at_restaurant() {
let order1 = back_of_house::Appetizer::Soup;
let order2 = back_of_house::Appetizer::Salad;
}
绝对路径
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
pub fn eat_at_restaruant() {
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
}
相对路径
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use self::front_of_house::hosting;
pub fn eat_at_restaruant() {
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
}
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
map.insert(1, 2);
}
use std::fmt;
use std::io;
fn function1() -> fmt::Result {
//
}
fn function2() -> io::Result<()> {
//
}
use std::fmt::Result;
use std::io::Result as IoResult;
fn function1() -> Result {
//
}
fn function2() -> IoResult<()> {
//
}
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaruant() {
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
}
Cargo.toml
[dependencies]
rand = “0.5.5”
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 101);
}
use std::collections::HashMap;
use std::cmp::Ordering;
use std::io;
use std::{cmp::Ordering, io};
use std::io;
use std::io::Write;
use std::io::{self ,Write};
use std::collections::*;
src/lib.rs
mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaruant() {
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
hosting::add_wo_waitlist();
}
src/front_of_house.rs
pub mod hosting;
src/front_of_house/hosting.rs
pub fn add_wo_waitlist() {}