2

I am used to organising files in separate directories depending on the problem domain (for example grouping image processing stuff together, IO stuff in another directory etc). I am not sure if this way or organisation is even recommended in Rust.

I have setup my project in multiple directories:

- helloworld
    - Cargo.toml
    - src
        - main.rs
        - test
            - one.rs

I am trying to use a function from one.rs in main.rs

one.rs

fn test() {
    println!("Calling test...");
}

main.rs

use test::one::*;

fn main() {
    println!("Hello, world!");
    test();
}

This results in a compile time error:

error[E0432]: unresolved import `test::one::*`
 --> src/main.rs:1:5
  |
1 | use test::one::*;
  |     ^^^^^^^^^^^^^ Maybe a missing `extern crate test;`?

error[E0425]: cannot find function `test` in this scope
 --> src/main.rs:6:5
  |
6 |     test();
  |     ^^^^ not found in this scope

Looking at some online projects, it seems like something like this should be possible.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Luca
  • 10,458
  • 24
  • 107
  • 234
  • 1
    Please take the time to read [*The Rust Programming Language*](https://doc.rust-lang.org/stable/book/second-edition/). It has an [entire section on how modules live on the filesystem](https://doc.rust-lang.org/stable/book/second-edition/ch07-01-mod-and-the-filesystem.html). – Shepmaster Aug 11 '17 at 14:16
  • 1
    Have you read [the "Modules" chapter in the Rust book](https://doc.rust-lang.org/book/second-edition/ch07-00-modules.html)? I'd really recommend you to read all of it to understand the whole module system. The tl;dr is: you need `mod` declarations. [See here, too](https://stackoverflow.com/a/43262123/2408867). And if interested: this might change in the future -- there is a lot of discussion about how to redesign Rust's modules. – Lukas Kalbertodt Aug 11 '17 at 14:17
  • 1
    Thank you for these pointers. I just started using it and looking at the online book. Was just jumping a bit ahead there – Luca Aug 11 '17 at 14:22

1 Answers1

2

It is possible, however you need to inform your code about additional module that is inside your code using:

mod test;

And then create additional file

// src/test/mod.rs

pub mod one;
Hauleth
  • 22,873
  • 4
  • 61
  • 112