Can't Figure out how to use modules in the same directory

The directory structure of my project looks like this:

.
├── Cargo.lock
├── Cargo.toml
└── src
    ├── algorithms
    │   ├── hopcroft_ac.rs
    │   ├── mod.rs
    │   ├── partition.rs
    │   ├── relations.rs
    │   ├── simgpp_ac.rs
    │   ├── simulation_ac.rs
    │   └── simulation_alg.rs
    ├── automata
    │   ├── determinize_ac.rs
    │   ├── dfa.rs
    │   ├── hopcroft_ac.rs
    │   ├── mod.rs
    │   ├── nfa.rs
    │   ├── simquotient_ac.rs
    │   └── toy_nfa.rs
    ├── bin
    │   └── main.rs
    ├── lib.rs
    ├── parser
    │   ├── mod.rs
    │   ├── timbuk.pest
    │   └── timbuk.rs
    └── utils
        ├── graphviz.rs
        ├── mod.rs
        └── to_str_sep.rs

6 directories, 24 files

I want to be able to use the file relations.rs from within both simulation_ac.rs and simgpp_ac.rs.

So, when I put in the line use super::relations::*; in simgpp_ac.rs everything works fine. When I put in the line use super::relations::*; in simulation_ac.rs, I get this error:

error[E0433]: failed to resolve: there are too many leading `super` keywords
 --> src/algorithms/simulation_ac.rs:6:5
  |
6 | use super::relations::*;
  |     ^^^^^ there are too many leading `super` keywords
1 Like

This is not enough of your source to identify the problem. Under normal circumstances, use super::relations::* should work in both of those files.

What command are you running to compile the code?

What does src/algorithms/mod.rs contain?

2 Likes

I generally let the rust-analyzer extension in VSCode tell me what the errors are. But I have also tried with cargo build.

The file src/algorithms/mod.rs contains the following:

pub mod hopcroft_ac;
pub mod simulation_ac;
pub mod simulation_alg;
pub mod simgpp_ac;
pub mod partition;
pub mod relations;

The error “there are too many leading super keywords” suggests that something, somewhere, is trying to compile simulation_ac.rs as the root file of a crate (because that's the only situation I know of where no super module exists).

Can you show us your Cargo.toml? If it has any path = "src/algorithms/simulation_ac.rs" in it, this would explain that error.

1 Like

There is some assymetry between the simulation_ac.rs and the simgpp_ac.rs file indeed. There is a binary linked to one, but not another.

I have the Cargo.toml file here:

[package]
name = "reduction_code"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
chrono = "0.4.22" # for display .svg files timestamps
pest = "2.4.0" # For parsing Timbuk format
pest_derive = "2.4.0" # For parsing Timbuk format

[[bin]]
name = "main"
path = "src/bin/main.rs"

[[bin]]
name = "hopcroft-bench"
path = "src/algorithms/hopcroft_ac.rs"

[[bin]]
name = "simpreorder-bench"
path = "src/algorithms/simulation_ac.rs"

Yes, this is the problem. In general, you should not use one source file for two different crates (here your library and a binary). They will be compiled with different surrounding items, and it will be hard to get a successful and warning-free compilation. In this case, not only is super not valid, the item you were actually trying to access, relations, doesn't actually exist at all when compiling the binary.

Instead, write a separate file for the binary (preferably at the auto-discovery path src/bin/simpreorder-bench.rs and now you don't need any path) and have it access the items from the library (use reduction_code::algorithms::simulation_ac;).

As a general rule: if you are doing something that seems to require specifying a path = "..." to a source file, in either Cargo.toml or in your source code's mods, then you may be making a mistake.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.