Rust modules structure in a nutshell

Rust module structure in a nutshell.

File tree:

.
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── main.rs
│   ├── my_modules_in_rootdir.rs
│   ├── my_modules_with_modrs
│   │   ├── mod.rs
│   │   ├── submod_a.rs
│   │   └── submod_b.rs
│   ├── my_modules_without_modrs
│   │   ├── submod_1.rs
│   │   └── submod_2.rs
│   └── my_modules_without_modrs.rs
\

Module tree (this nice module tree is generated by cargo-modules)

crate modules
├── mod my_module_in_main: pub(crate)
│   └── mod my_mod_in_main_nested: pub
├── mod my_modules_in_rootdir: pub(crate)
├── mod my_modules_with_modrs: pub(crate)
│   ├── mod submod_a: pub
│   └── mod submod_b: pub
└── mod my_modules_without_modrs: pub(crate)
    ├── mod submod_1: pub
    └── mod submod_2: pub

/src/main.rs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#![allow(dead_code)]
// declare module in the root dir, so that this module is added to the
// module tree
mod my_modules_in_rootdir; 
mod my_modules_with_modrs;
mod my_modules_without_modrs;
mod my_module_in_main {
    // DESCRIPTION IS PRIVATE TO my_mod
    const DESCRIPTION: &str = "This module is defined as `mod my_mod_in_main{{...}}` in main.rs";
    pub fn desc() {
       println!("{}", DESCRIPTION);
    }
    }
    pub mod my_mod_in_main_nested {
        // DESCRIPTION PRIVATE TO my_mod::my_mod_nested
        const DESCRIPTION: &str = "This module is defined as a nested mod in my_mod_in_main` in main.rs";
        pub fn desc() {
           println!("{}", DESCRIPTION);
        }
    }
}

fn main() {
    println!("Hello, world from main()");
    my_module_in_main::desc();
    my_module_in_main::my_mod_in_main_nested::desc();
    my_modules_in_rootdir::desc();
    my_modules_with_modrs::desc();
    my_modules_with_modrs::submod_a::desc();
    my_modules_with_modrs::submod_b::desc();
    my_modules_without_modrs::submod_1::desc();
    my_modules_without_modrs::submod_2::desc();
}

/src/my_modules_in_rootdir

1
2
3
4
const DESCRIPTION: &str = "This module is defined in src/my_modules_in_rootdir.rs, and declared in main.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}

/src/my_modules_without_modrs.rs

1
2
3
4
// This is bad, you need this file in root dir to declare the module into the module tree anyways,
// why don't you just move this file to somemodule/mod.rs ?? 
pub mod submod_1;
pub mod submod_2;

/src/my_modules_without_modrs/submod_1.rs

1
2
3
4
const DESCRIPTION: &str = "This module is /src/my_modules_without_modrs/submod_1, module declared in /src/my_modules_without_modrs.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}

/src/my_modules_without_modrs/submod_2.rs

1
2
3
4
const DESCRIPTION: &str = "This module is /src/my_modules_without_modrs/submod_2, module declared in /src/my_modules_without_modrs.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}

/src/my_modules_with_modrs/mod.rs

1
2
3
4
5
6
7
pub mod submod_a;
pub mod submod_b;

const DESCRIPTION: &str = "This module is /src/my_modules_with_modrs, function declared in mod.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}

/src/my_modules_with_modrs/submod_a.rs

1
2
3
4
const DESCRIPTION: &str = "This module is /src/my_modules_with_modrs/submod_a, module declared in mod.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}

/src/my_modules_with_modrs/submod_b.rs

1
2
3
4
const DESCRIPTION: &str = "This module is /src/my_modules_with_modrs/submod_b, module declared in mod.rs";
pub fn desc() {
    println!("{}", DESCRIPTION);
}
Jan 5, 2023


[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<




Fun with Image Maps and SVGs via benji February 20, 2024

Over the past few weeks I've been playing around with making some images on my website interactive. My "informatics" class in high school taught us some basic HTML. By basic I mean in notepad and writing everything by hand, saving to a...

Goblin Week 2024 via Helvetica Blanc January 26, 2024

It snuck up on me, but I managed to draw my little goblins to celebrate the week! I love my children - they're like awful Pikmin. I've uploaded the whole parade as a print, as well as individual prints for each goblin. There's something very fu…

How to trust gpg keys via Travis Shears Personal Site October 27, 2023

After moving some GPG keys to a new computer I kept getting these trust warnings. It is NOT certain that the key belongs to the person named in the user ID.If you * really * know what you are doing, you may answer the next question with yes. Use this key a…

Generated by openring from webring