Reputation: 322
I am writing a TUI app in rust to manage some game servers. The application itself works fine but the main.rs file is now over 1k lines so I decided to split it into multiple files to make it easier to work with. Each of the files needs to be able to access the structs and functions in the other files. The split files have the following structure.
src \
main.rs
render.rs
ark.rs
db.rs
This is my current Cargo.toml
[package]
name = "ark-manager"
version = "0.1.0"
authors = ["Nan0Scho1ar <[email protected]>"]
edition = "2018"
[dependencies]
crossterm = { version = "0.19", features = [ "serde" ] }
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"] }
rand = { version = "0.7.3", default-features = false, features = ["std"] }
tui = { version = "0.14", default-features = false, features = ['crossterm', 'serde'] }
thiserror = "1.0"
I tried following the documentation I found here. https://doc.rust-lang.org/stable/book/ch07-05-separating-modules-into-different-files.html
Which led me to a believe the solution looks something like this.
main.rs
mod render;
mod db;
mod ark;
pub use crate::render::*;
pub use crate::db::*;
pub use crate::ark::*;
//Main loop etc
...
ark.rs
pub mod ark {
pub use crate::render::*;
pub use crate::db::*;
//Structs, enums etc
pub enum Event<I> {
Input(I),
Tick
}
...
}
db.rs
pub mod db {
pub use crate::render::*;
pub use crate::ark::*;
//DB functions
...
}
render.rs
pub mod render {
pub use crate::db::*;
pub use crate::ark::*;
//Render functions
...
}
I also updated to the references to the things which are now in separate modules
//E.G.
if let Ok(_) = tx.send(Event::Tick) {
//Becomes
if let Ok(_) = tx.send(ark::Event::Tick) {
//And
Event::Input(event) => match event.code {
//Becomes
ark::Event::Input(event) => match event.code {
But this led to the following errors.
error[E0433]: failed to resolve: could not find `Event` in `ark`
--> src/main.rs:79:45
|
79 | if let Ok(_) = tx.send(ark::Event::Tick) {
| ^^^^^ could not find `Event` in `ark`
error[E0433]: failed to resolve: could not find `Event` in `ark`
--> src/main.rs:216:22
|
216 | ark::Event::Input(event) => match event.code {
| ^^^^^ could not find `Event` in `ark`
error[E0433]: failed to resolve: could not find `Event` in `ark`
--> src/main.rs:226:22
|
226 | ark::Event::Tick => {}
| ^^^^^ could not find `Event` in `ark`
error[E0433]: failed to resolve: could not find `Event` in `ark`
--> src/main.rs:230:22
|
230 | ark::Event::Input(event) => match event.code {
| ^^^^^ could not find `Event` in `ark`
...
After trying to get this to work for some time I decided to see how other people solved this issue and I found a number of answers to questions on stack overflow such as this one. Rust import error I tried to use imports outlined in the accepted answer which looks something like this.
main.rs
mod render;
mod db;
mod ark;
//Main loop etc
...
ark.rs
use render;
use db;
//Structs, enums etc
...
db.rs
use render;
use ark;
//DB functions
...
render.rs
use db;
use ark;
//Render functions
...
But this structure leads to the following errors.
error[E0432]: unresolved import `db`
--> src/render.rs:1:9
|
1 | use db;
| ^^ no external crate `db`
error[E0432]: unresolved import `ark`
--> src/render.rs:2:9
|
2 | use ark;
| ^^^ no external crate `ark`
error[E0432]: unresolved import `render`
--> src/db.rs:1:9
|
1 | use render;
| ^^^^^^ no external crate `render`
error[E0432]: unresolved import `ark`
--> src/db.rs:2:9
|
2 | use ark;
| ^^^ no external crate `ark`
error[E0432]: unresolved import `render`
--> src/ark.rs:1:9
|
1 | use render;
| ^^^^^^ no external crate `render`
error[E0432]: unresolved import `db`
--> src/ark.rs:2:9
|
2 | use db;
| ^^ no external crate `db`
...
What is the correct method for splitting my main.rs file into 4 smaller files, such that each of the files can use the structs and functions in the other files.
Upvotes: 1
Views: 678
Reputation: 117641
In main:
mod render;
mod db;
mod ark;
Then in everything else:
use crate::render;
use crate::db;
// ...
Upvotes: 2