After reading TRPL chapter about modules and short discussion on IRC I still have no understanding of how Rust's module system works (especially how to apply it to restructure my project). I'll try to describe flow of my thoughts and it would be great if someone could explain me further steps.
I'm writing a game client and have src/main.rs with struct Client in it.
Client instantiates struct State, trait Ai and trait Driver. So I have something like this:
struct Client {
state: State, // stores client state
ai: Ai, // something implementing Ai trait
// to make decisions based on State
driver: Driver, // underlying networking
}
struct State { ... }
trait Ai {
fn update (state: &State);
}
struct SimpleAi { ... }
impl Ai for SimpleAi { ... }
struct ComplexAi { ... }
impl Ai for ComplexAi { ... }
trait Driver { ... }
struct MioDriver { ... }
impl Driver for MioDriver { ... }
and because of 2000+ lines of code I want to split this one to smaller pieces in separate files.
Separation is obvious (or not?):
src/main.rs:
struct Client {
state: State, // stores client state
ai: Ai, // something implementing Ai trait
// to make decisions based on State
driver: Driver, // underlying networking
}
src/ai.rs:
trait Ai {
fn update (state: &State);
}
src/ai_simple.rs:
struct SimpleAi { ... }
impl Ai for SimpleAi { ... }
src/ai_complex.rs:
struct ComplexAi { ... }
impl Ai for ComplexAi { ... }
src/driver.rs
trait Driver { ... }
src/driver_mio.rs
struct MioDriver { ... }
impl Driver for MioDriver { ... }
I suppose that I can't just split my code to separate files. I do not understand "mod" exact meaning. By analogy with C or C++ or Python I expected something like "this is module foo" or "this is part of module foo" or "include module foo here" but it's not. It's something different.
Could someone please explain what should I add to this files to make them compile and (what is more important) why?
Thank you.