Search⌘ K

Organizing Imports with a Prelude

Explore how to simplify module imports in Rust game development by creating a custom prelude. Understand using the mod keyword, re-exporting modules, and managing constants to improve code organization in a dungeon crawler project.

We'll cover the following...

Prefixing every map access with map:: or crate::map:: becomes continuously cumbersome as we add more modules.

When we access a Rust library, it’s common for the library author to have placed everything we need in a convenient prelude. We used bracket-lib’s prelude in Hello Bracket Terminal. We can simplify module access by making our own prelude to export common functionality to the rest of the program.

Add the following to our main.rs file:

Rust 1.40.0
mod map;
mod prelude {
pub use bracket_lib::prelude::*;
pub const SCREEN_WIDTH: i32 = 80;
pub const SCREEN_HEIGHT: i32 = 50;
pub use crate::map::*;
}
use prelude::*;
  • Line 1: By ...