2

I'm trying to use the functions from one file with multiple other files.

When I try adding 'mod somefile' to the files, the Rust compiler wants them to be nested in a subfolder, which isn't how I want to structure the project, as it would mean duplicating the file each time.

// src/main.rs
mod aaa;
mod bbb;

fn main() {
    aaa::do_something();
    bbb::do_something_else();
}
// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
    zzz::do_stuff();
}
// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
    zzz::do_stuff();
}
// src/zzz.rs

pub fn do_stuff() {
    // does stuff here
}

I would like to be able to leave src/zzz.rs in the root src folder and use its functions among any of the other files in the project, instead of having to duplicate it in subfolders for each file (ex: src/aaa/zzz.rs, src/bbb/zzz.rs).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • There are 2 different schools of thought competing. One does not see a problem with having module hierarchy to be locked in on file system hierarchy, and they even think it is a good idea. The other school of thought hates that to the bone. Java (and rust) follow the former school. So in your case, you have to build a crate from the code you want to reuse from within multiple other crates. – BitTickler Jan 18 '19 at 20:12
  • Possible duplicate of [Rust basic imports (includes)](https://stackoverflow.com/questions/26224947/rust-basic-imports-includes) – hellow Jan 18 '19 at 21:21
  • @BitTickler Java's packages and Rust's modules are very different. Unless you compare them to something even more different (like Python's modules or bash's sourced files), Java's packages and Rust's modules seem more like exact opposites. Java's packages are one-package-per-directory, packages don't nest, every file belongs to exactly one package etc. In Rust, modules nest and you can have multiple modules per file, so I don't see why they should belong to the same "school of thought"? – Andrey Tyukin Jan 18 '19 at 21:22
  • @AndreyTyukin They are, in the sense that file location matters. You might guess - and rightfully so, that I am not really a fan of that. – BitTickler Jan 18 '19 at 21:46

1 Answers1

5

You need mod zzz; only once in the main.rs.

In aaa.rs and bbb.rs you need a use crate::zzz;, not a mod zzz;.

An example:

File src/aaa.rs:

use crate::zzz; // `crate::` is required since 2018 edition

pub fn do_something() {
    zzz::do_stuff();
}

File src/bbb.rs:

use crate::zzz;

pub fn do_something_else() {
    zzz::do_stuff();
}

File src/main.rs:

// src/main.rs
mod aaa;
mod bbb;
mod zzz;

fn main() {
    aaa::do_something();
    bbb::do_something_else();
}

File src/zzz.rs:

pub fn do_stuff() {
    println!("does stuff zzz");
}

You would need a mod zzz; inside of aaa module only if you had a directory called aaa and inside of it a files mod.rs and zzz.rs. Then you would have to put mod zzz; in mod.rs to make the submodule aaa::zzz visible to the rest of your program.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93