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.