title: How to include files from same directory in a module using Cargo/Rust?
body: <p>I have a Cargo project consisting of three files in the same directory: <code>main.rs</code>, <code>mod1.rs</code> and <code>mod2.rs</code>.  </p>
  
  <p>I want to import functions from <code>mod2.rs</code> to <code>mod1.rs</code> the same way I would import functions from <code>mod1.rs</code> to <code>main.rs</code>.<br>
  I've read about the file structure required but I don't get it - naming all the imported files <code>mod</code> will lead to minor confusion in the editor and also this just complicates the project hierarchy.  </p>
  
  <p>Is there a way to import/include files independently of directory structure as I would in Python or C++? </p>
  
  <p>main.rs:</p>
  
  <pre><code>mod mod1; // Works
  
  fn main() {
      println!("Hello, world!");
      mod1::mod1fn();
  }
  </code></pre>
  
  <p>mod1.rs:</p>
  
  <pre><code>mod mod2; // Fails
  
  pub fn mod1fn() {
      println!("1");
      mod2::mod2fn();
  }
  </code></pre>
  
  <p>mod2.rs:</p>
  
  <pre><code>pub fn mod2fn() {
      println!("2");
  }
  </code></pre>
  
  <p>Building results in:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: cannot declare a new module at this location
   --&gt; src\mod1.rs:1:5
    |
  1 | mod mod2;
    |     ^^^^
    |
  note: maybe move this module `src` to its own directory via `src/mod.rs`
   --&gt; src\mod1.rs:1:5
    |
  1 | mod mod2;
    |     ^^^^
  note: ... or maybe `use` the module `mod2` instead of possibly redeclaring it
   --&gt; src\mod1.rs:1:5
    |
  1 | mod mod2;
    |     ^^^^
  </code></pre>
  
  <p>I can't <code>use</code> it as it doesn't exist as a module anywhere, and I don't want to modify the directory structure.</p>