I would like some guidance setting up a project and modules.
I would like to have a library that supplies a small set of types, such as Context, Request, Identifier, ... I prefer:
- Single file for each type (context.rs, request.rs, identifier.rs)
- Access each type with "use kernel::Context", "use kernel::Request", etc.
I know how to accomplish this if I put all the types in a single file; I am stuck on how to have each type in its own file.
Chapter 7 Section 5 of the Rust Book demonstrates this.
Here's an example approach based on the names you've provided:
src/kernel/mod.rs or src/kernel.rs (both work the same):
mod context; // declare `context` module private to `types` module
mod request; // declare `request` module private to `types` module
// re-export all exports from `context` and `request` from `types`
pub use self::{context::*, request::*};
src/kernel/context.rs:
pub struct Context {
// ...
}
src/kernel/request.rs:
// use the `Context` struct from `context.rs`
// only visible in `request.rs`, use `pub use` to re-export
use super::Context;
pub struct Request {
// ...
}
src/main.rs:
pub mod kernel;
fn main() {
let request = kernel::Request {
//...
};
// ...
}
I suggest more targeted re-exports over ::*. Unless you really intend for the entire namespace to get flattened.
// In the parent module
pub use context::Context;
pub use request::Request;
// ...
Thank you very much for this help. I was stuck on the re-exporting part. This is exactly what I need.