'Use Outer Reference Inside Module
Consider:
struct Outer { ... }
mod Inner {
let outer: Outer { ... }
}
How does one "import" Outer into Inner? As it stands, Rust will complain no external crate "Outer".
Attempted solutions include mod { use Outer; ... }.
Solution 1:[1]
First, in Rust, you can always use qualified paths. That means if Outer as a type didn't work, use Outer will not work either.
To access the parent module, use the keyword super:
let outer: super::Outer;
It refers to the parent module, so we say "from the parent module, take Outer".
Another way, not recommended for this case but useful to know in other cases, is to track the parent module path down from the crate root. Assuming your parent module is inside the module b that is inside a, you can write crate::a::b::parent_module::Outer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chayim Friedman |
