Guidance on module setup

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 (,,
  • 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/ or src/ (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::*};


pub struct Context {
  // ...


// use the `Context` struct from ``
// only visible in ``, use `pub use` to re-export
use super::Context;

pub struct Request {
  // ...


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.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.