1

When I use the path way to import a module, I face a strange problem. The code may look like this:

#[path = "../../models.rs"]
mod models;

This would work. But somebody told me that this way is not recommended. To better understand the Rust import, I have the following questions:

  • In what situation we should using #[path = "../../xxx.rs"] to import a module?
  • What are the advantages and disadvantages of this way to import module?
  • Should we avoid using this way to import module in future?

I searched the Internet, but nobody seem to have these questions.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • Mostly it's just cognitive load. If the file structure reflects the logical structure of your modules, the codebase is much easier to understand. Many headaches, surprising behaviour and bugs can ensue when there's a disconnect; refactoring also becomes more difficult. It also (accidentally?) encourages reusing the same source in multiple places, which probably isn't the intention (better import and/or rexport the module from a single origin). – eggyal Sep 14 '21 at 14:23
  • STOP USE PATH omg, why people suddenly use a feature I NEVER SAW IN 5 YEARS – Stargateur Sep 14 '21 at 14:47
  • please tell why – Dolphin Sep 14 '21 at 14:49
  • 1
    Some features exist for advanced, non-standard uses. If you have to ask when you should use that, the answer is that _you_ should not use it at all. Think of it as a _expert mode only_. – rodrigo Sep 14 '21 at 14:54
  • 2
    That somebody was probably me, I linked this Q&A in one of your previous questions: [Why do I get "expected struct file1::A found struct file2::A" error when using a struct in multiple files?](https://stackoverflow.com/questions/68928911/why-do-i-get-expected-struct-file1a-found-struct-file2a-error-when-using-a) Does it not answer your question? Though I'll try to write up an answer here for when you *should* or *could* reasonably use it. – kmdreko Sep 14 '21 at 16:01
  • That why I mentioned some strange problem, that problem refer to the problem I already asked. But I still not understand it completely,I just know we should not do it like that, but did not know why we should not do like that. @kmdreko just avoid the problem I was facing previously? I do not think so. – Dolphin Sep 14 '21 at 16:25

1 Answers1

1

In what situation we should using #[path = "../../xxx.rs"] to import a module?

It is, as far as I know, never necessary. It could be a way to handle a special situation such as using conditional compilation to import different implementations of a single module, but you can also use pub use to get that effect. For example, the standard library uses both techniques in the same place, for some reason.

What are the advantages and disadvantages of this way to import module?

It lets you place a module's source code in a non-standard location. The disadvantages are that you are placing it in a non-standard location (which may be surprising to readers, including yourself) and that it makes it easy to accidentally duplicate a module.

The second point is the main reason why you hear not to use it: misunderstanding how the module system works leads to people trying to use #[path] to use a module from multiple other modules, and they instead end up duplicating the module, leading to strange errors as the module gets compiled twice in different contexts.

#[path] is an “advanced” feature for special circumstances. It is never necessary to write normal Rust code no matter how many files the code is divided into, and it can be confusing to readers, and therefore it should be avoided unless necessary.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • 2
    The "for some reason" seems to be that on non-Windows platforms they *only* want to bring in the `compat` module, which is a sub-module of the `windows` module. It might not be possible to do `pub use` there because there may be Windows-specific code in those other components. I imagine it would cause issues if you tried to declare the `windows` module on a non-Windows system, even if it were simply to `pub use self::windows::compat` – Herohtar Sep 14 '21 at 21:39