0

I'm trying to understand how to properly setup imports, with two goals in mind:

  1. Every script should be compilable using rustc
  2. Building via cargo is supported swell.

With absolute paths it seems to work. I cannot get it to work with relative paths. However it seems wrong to use absolute paths.

Whats the correct way to handle imports?

Lets take two examples into account:

1. Example

src
├── main.rs
├── root
│   └── nested.rs
└── root.rs

abs paths

// main.rs:
#[path="root/nested.rs"] 
mod nested 
#[path="root.rs"] 
mod root

// root.rs:
#[path="root/nested.rs"] 
mod nested

rel paths

// main.rs:
mod root; 
use crate::root::nested;

// root.rs --\> cannot be compiled using `rustc`.
pub mod nested;

2. Example

src
├── main.rs
├── notnested.rs
└── root.rs

abs paths

// main.rs:
#[path="notnested.rs"] 
mod notnested 
#[path="root.rs"] 
mod root

// root.rs:
#[path="notnested.rs"] 
mod notnested

rel paths

// main.rs:
mod root 
mod notnested

// root.rs --\> cannot be compiled using `rustc`.
use crate::notnested;
cgtinker
  • 11
  • 2
  • 2
    Why `rustc`? Any non-trivial project benefits considerably from `cargo`. – tadman Jan 25 '23 at 16:41
  • 1
    I'm struggling to understand the dependencies between these files and *why* you are doing it this way. It seems like you want `main.rs` and `root.rs` to be executables, but then why is `main.rs` importing `root.rs`? – kmdreko Jan 25 '23 at 16:48
  • The layout is just a random example - I just want to figure out how to handle such scenarios. I really liked how scenarios like that worked with `bazel` - probably that's just not possible. root_2 was a typo - fixed it :) – cgtinker Jan 25 '23 at 17:00
  • *"With absolute paths it seems to work... However it seems wrong to use absolute paths."* - this is because your examples with the `#[path = ...]` attributes are flawed. It may work depending on what you're doing, but if compiling `main.rs`, you'll have duplicate modules `crate::nested` and `crate::root::nested`. If you have types defined in your `nested` module and you try to use `crate::root::nested::Type` in a place expecting `crate::nested::Type` for example (or vice versa), it will be a compiler error complaining they are different types even when they come from the same file. – kmdreko Jan 25 '23 at 17:03
  • 1
    *"The layout is just a random example"* - then what is the question you're really asking if this is not a practical application? Are you asking how to have multiple binaries with shared logic? There's ways to do that. – kmdreko Jan 25 '23 at 17:07
  • Interesting point. The idea is basically to have modules within a module without creating a lib. But it seems like creating libs and using them as dependencies is the way to go. – cgtinker Jan 25 '23 at 17:11
  • 1
    @cgtinker single cargo package can contain one library crate and any number of binaries. So it is likely that you don't even need to create standalone library packages to depend on. – Ivan C Jan 25 '23 at 17:13
  • @IvanC that would step on OPs ask that they should be compilable with `rustc` only, but that desire is dubious at best. I agree that a properly constructed cargo package is best. – kmdreko Jan 25 '23 at 17:20
  • Is there a proper cmd in cargo to execute a crate separately if it has a `fn main` or something similar? Probably I just have the wrong cmd in mind. – cgtinker Jan 25 '23 at 17:28
  • `cargo run --bin $binary_name` – Ivan C Jan 25 '23 at 17:30
  • sorry formulated my question badly. is it possible to compile a `.rs` file which includes an import using `use crate::_` and a `fn main`? – cgtinker Jan 25 '23 at 17:34
  • Does this answer your question: https://stackoverflow.com/a/45520092/5397009 ? (Look also at [the](https://stackoverflow.com/q/26224947/5397009) [linked](https://stackoverflow.com/q/22596920/5397009) [duplicates](https://stackoverflow.com/q/26388861/5397009)) – Jmb Jan 26 '23 at 08:07

1 Answers1

0

I've got a repo here: https://github.com/cadolphs/aoc_2022/tree/main/src

I used Rust for the Advent of Code 2022. In this project, I have the main.rs for the binary. I have individual modules for each day of the challenge.

Some of these modules are single-file modules (day1.rs) and other modules are in directories like day11. Note that the "entry point" to a module in a folder should be called mod.rs for the import to work normally.

This structure should work for what you're trying to do.

cadolphs
  • 9,014
  • 1
  • 24
  • 41