Reputation: 33
Firt of all, I am a total begginner in Rust, I started to use a code analyzer (Mega-Linter) and it made me realize how much I duplicated the same "use" statements in my submodules. Here what my source file tree looks like :
src/
- lib.rs
- ui/
- mod.rs
- ui_mod_1.rs
- ui_mod_2.rs
Then I realized that my ui_mod_1.rs and ui_mod_2.rs had almost the same bunch of "use" statements :
// ui_mod_1.rs
use tui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
};
// rest of the file
// ui_mod_2.rs
use tui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
};
// rest of the file
// mod.rs
pub mod ui_mod_1;
pub mod ui_mod_2;
// lib.rs
pub mod ui;
The idea behind ui_mod_1.rs and ui_mod_2.rs is to split "ui utilitaries" functions by theme to avoid having a huge source file containing all of them. A possible solution is to merge the two files, but this is not what I want to do.
What I tried is to move the "use" that the two submodules have in common in the mod.rs or even in the lib.rs like so :
// mod.rs
pub use tui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
};
pub mod ui_mod_1;
pub mod ui_mod_2;
But this does not work. After some research I still did not find how to do this. Is there an elegant way to regroup "use" statements for all submodules ?
Upvotes: 3
Views: 187
Reputation: 23264
You can create a ui_prelude
module that contains the use statements as pub use
, and then do just use ui_prelude::*
in your modules:
// ui_prelude.rs
pub use tui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
};
// ui_mod_1.rs and ui_mod_2.rs
use super::ui_prelude::*;
// mod.rs
mod ui_prelude.
pub mod ui_mod_1;
pub mod ui_mod_2;
Upvotes: 5