1

I have a file Projectile.rs that is in the src directory. It is currently used by main.rs. However, I require the file FreeFall.rs sharing the same directory to be used in Projectile.rs. Here is how it looks at the moment:

DIRECTORY

src
 |___main.rs
 |___Projectile.rs
 |___FreeFall.rs

MAIN.RS

mod Projectile;
fn main() {
    println!("Goed... momenteel");
    Projectile::projectile(10.0, 5.0, 100.0, 7.5);
}

PROJECTILE.RS

use std;
mod FreeFall;
pub fn projectile(init: f64, AngleX: f64, AngleY: f64, AngleZ: f64) {
    let mut FFAccel = FreeFall::acceleration();
    struct Velocities {
        x: f64,
        y: f64,
        z: f64,
        t: f64,
    };
    let mut Object1 = Velocities {
        x: init * AngleX.sin(),
        y: init * AngleY.cos(),
        z: init * AngleZ.tan(),
        t: (2.0 * init.powf(-1.0) * AngleY.sin()) / FFAccel,
    };
    println!("{}", Object1.t);
}

FREEFALL.RS

use std;

pub fn acceleration() {
    // maths here
}

I can't just use the value 9.81 (which is the average gravity on Earth) because it does not take into account air resistance, terminal velocity, etc.

I tried including the FreeFall module into main.rs instead, but it did not work.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    You need to move `freefall.rs` into a subdirectory called `projectile`. Also note that `use std;` doesn't do anything, and that rust module names are lower case by convention. – Sven Marnach Mar 14 '19 at 21:14
  • Thank you, much appreciated – Andrew Chon Mar 14 '19 at 21:29
  • 1
    You should make an effort not to confuse *files* with *modules*. Files may contain multiple modules (but modules may not be spread across multiple files, as in some other languages). You could, for example, put the contents of `freefall.rs` directly within `mod freefall { /* here */ }`, and not have to create a new file. – trent Mar 14 '19 at 21:45
  • 1
    Idiomatic Rust uses `snake_case` for variables, methods, macros, fields and modules; `UpperCamelCase` for types and enum variants; and `SCREAMING_SNAKE_CASE` for statics and constants. Use `projectile`; `free_fall` instead, please. – Shepmaster Mar 15 '19 at 00:02

1 Answers1

3

In Rust, single-file modules can't declare other modules from other files with the mod keyword. To do this, you need to create a directory for the module, name the module root file mod.rs, and create a separate file (or directory) inside that one for each nested module.

As a rule of thumb, you should declare every module in the "root file" (typically main.rs, lib.rs or mod.rs) of the directory, and use it everywhere else. Conveniently, you can use crate:: as the root module of your crate, and refer to your modules using that.


For example:

src/
  main.rs             crate
  foo.rs              crate::foo
  bar.rs              crate::bar
  baz/
    mod.rs            crate::baz
    inner.rs          crate::baz::inner

main.rs

// declare the modules---we only do this once in our entire crate
pub mod foo;
pub mod bar;
pub mod baz;

fn main() { }

foo.rs

// use other modules in this crate
use crate::bar::*;
use crate::baz::inner::*;

baz/mod.rs

// the baz module contains a nested module,
// which has to be declared here
pub mod inner;

Some resources for further reading:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Frxstrem
  • 38,761
  • 9
  • 79
  • 119