Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
Separating Modules into Different Files
Separating Modules into Different Files
For example,move the
front_of_house
module to its own file src/front_of_house.rssrc/lib.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();
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
src/front_of_house.rs
pub mod hosting {
pub fn add_to_waitlist() {}
}
pub fn add_to_waitlist() {}
}
To continue with our example and extract the
hosting
module to its own file as well, we change src/front_of_house.rs to contain only the declaration of the hosting
module:src/front_of_house.rs
pub mod hosting;
Then we create a src/front_of_house directory and a file src/front_of_house/hosting.rs to contain the definitions made in the
hosting
module:src/front_of_house/hosting.rs
pub fn add_to_waitlist() {}