title: How do you use parent module imports in Rust?
body: <p>If you have a directory structure like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>src/main.rs
  src/module1/blah.rs
  src/module1/blah2.rs
  src/utils/logging.rs
  </code></pre>
  
  <p>How do you use functions from other files?</p>
  
  <p>From the Rust tutorial, it sounds like I should be able to do this:</p>
  
  <p><strong>main.rs</strong></p>
  
  <pre><code>mod utils { pub mod logging; }
  mod module1 { pub mod blah; }
  
  fn main() {
      utils::logging::trace("Logging works");
      module1::blah::doit();
  }
  </code></pre>
  
  <p><strong>logging.rs</strong></p>
  
  <pre><code>pub fn trace(msg: &amp;str) {
      println!(": {}\n", msg);
  }
  </code></pre>
  
  <p><strong>blah.rs</strong></p>
  
  <pre><code>mod blah2;
  pub fn doit() {
      blah2::doit();
  }
  </code></pre>
  
  <p><strong>blah2.rs</strong></p>
  
  <pre><code>mod utils { pub mod logging; }
  pub fn doit() {
      utils::logging::trace("Blah2 invoked");
  }
  </code></pre>
  
  <p>However, this produces an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0583]: file not found for module `logging`
   --&gt; src/main.rs:1:21
    |
  1 | mod utils { pub mod logging; }
    |                     ^^^^^^^
    |
    = help: name the file either logging.rs or logging/mod.rs inside the directory "src/utils"
  </code></pre>
  
  <p>It appears that importing down the path, i.e. from <code>main</code> to <code>module1/blah.rs</code> works, and importing peers, i.e. <code>blah2</code> from <code>blah</code> works, but importing from the parent scope doesn't.</p>
  
  <p>If I use the magical <code>#[path]</code> directive, I can make this work:</p>
  
  <p><strong>blah2.rs</strong></p>
  
  <pre><code>#[path="../utils/logging.rs"]
  mod logging;
  
  pub fn doit() {
      logging::trace("Blah2 invoked");
  }
  </code></pre>
  
  <p>Do I really have to manually use relative file paths to import something from a parent scope level? Isn't there some better way of doing this in Rust?</p>
  
  <p>In Python, you use <code>from .blah import x</code> for the local scope, but if you want to access an absolute path you can use <code>from project.namespace.blah import x</code>.</p>