Rust学习笔记
Rust编程语言入门教程课程笔记
参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)
Lecture 7: Managing Growing Projects with Packages, Crates, and Modules
src/main.rs
// src/main.rs: the main file of the project, where the main function is defined; this is the crate root
// src/lib.rs: the root of your crate’s library; the library’s name is the same as the name of the crate
// src/bin: directory that can contain multiple binary crates; each file in this directory will be a separate binary crate
// src/bin/main.rs: the main file of the binary crate with the same name as the directory; this file is the crate root of the binary crate//Modules
use std::collections::HashMap;// use keyword to bring module into scopeuse std::io::Result as IoResult;// use keyword to bring module into scope
use std::{cmp, io};// use keyword to bring module into scope
use rand::Rng;// use keyword to bring module into scope
use std::collections::*;// use keyword to bring module into scope, * is glob operatorfn main() {let mut map = HashMap::new();map.insert(1, 2);println!("{:?}", map);let m = IoResult::Ok(());println!("{:?}", m);let mut rng = rand::thread_rng();let t = rng.gen_range(1..=10);println!("{:?}", t);
}
src/lib.rs
mod front_of_house;//{// module// pub mod hosting{// public module// pub fn add_to_waitlist(){}// }// mod serving{// private module// fn take_order(){}// fn serve_order(){}// fn take_payment(){}// }// fn fix_incorrect_order(){// cook_order();// super::serve_order();// super keyword to access parent module// crate::serve_order();// absolute path// }// pub fn cook_order(){}// 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::from("peaches"),// }// }// }
//}pub use crate::front_of_house::hosting;// use keyword to bring module into scope
//use crate::front_of_house::servering;// cannot use private module
//use front_of_house::hosting;// relative pathpub fn eat_at_restaurant(){// Absolute pathcrate::front_of_house::hosting::add_to_waitlist();// Relative pathfront_of_house::hosting::add_to_waitlist();// Order a breakfast in the summer with Rye toastlet mut meal = front_of_house::Breakfast::summer("Rye");// Change our mind about what bread we'd likemeal.toast = String::from("Wheat");println!("I'd like {} toast please", meal.toast);// The next line won't compile if we uncomment it; we're not allowed// to see or modify the seasonal fruit that comes with the meal// meal.seasonal_fruit = String::from("blueberries");hosting::add_to_waitlist();}fn serve_order(){}
src/front_of_house.rs
pub mod hosting; //{// public module
// pub fn add_to_waitlist(){}
// }mod serving{// private modulefn take_order(){}fn serve_order(){}fn take_payment(){}
}fn fix_incorrect_order(){cook_order();super::serve_order();// super keyword to access parent modulecrate::serve_order();// absolute path
}pub fn cook_order(){}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::from("peaches"),}}
}
src/front_of_house/hosting.rs
pub fn add_to_waitlist(){}