Cannot import Color from Vectors file

So I have three files- main.rs, vectors.rs, colors.rs
In vectors.rs I define a Vector struct and a Color alias for it.
In colors.rs I have a function that takes a Color as argument and prints something out.
Now the import in the colors.rs just doesn't work.
At the top of the colors.rs file I have this:

mod vectors;
use vectors::Color;

and at the top of main.rs (that doesn't give an error though):

mod vectors;
mod colors;
use vectors::Color;
use colors::*;

the compiler tells me to do this: dpaste/K8V7 (Python)
but that's not how i want my folders to look like.

You need only one mod name; for each module.

what do you mean?
I have 3 files, each one importing something different.
Also I don't see how that solves the import errors.

The colors file should be:

use crate::vectors::Color;

The main file:

mod vectors;
mod colors;
use crate::vectors::Color;
use crate::colors::*;

The crate:: is optional only in the same file as the mod statement. You should only ever mention each file once with a mod statement.

According to how you've written it, you actually have four modules:

  • A main module in main.rs
  • A vectors module, declared in main.rs and with its contents in vectors.rs
  • A colors module, declared in main.rd and with its contents in colors.rs
  • A colors::vectors module, declared in colors.rs and would have its contents in colors/vectors.rs (although that file presumably doesn't exist)

I think the use statement in colors.rs is interpreted relative to the current module, so it refers to colors::vectors::Color, which doesn't exist. You probably don't want the colors:: vectors module, so you can remove the mod statement in colors.rs, which declares it. You should also disambiguate the use in colors.rs to be relative to the crate root to ensure you are referencing the right thing:

use crate::vectors::Color;
1 Like

fixed. thank you