Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve the error "no module in the root" when using a module in Rust 2018?

I'm working on a project that is utilizing some local modules in folders under src/. I'm currently using Rust 2018 edition and one of the major changes for that is the handling of imports/use statements.

My module code is all working correctly, but as I started to pull it together to be used for the project as a whole I started getting this error:

error[E0432]: unresolved import `crate::lexer`
 --> src/main.rs:1:5
  |
1 | use crate::lexer;
  |     ^^^^^^^^^^^^ no `lexer` in the root

Currently, my code is set up like this:

src/
 | main.rs
 | lexer/
    | mod.rs
    | lexer.rs

lexer/lexer.rs

pub fn lex_stuff() -> Vec<String> { vec![String::new("test")] }

lexer/mod.rs

pub mod lexer;

main.rs

use crate::lexer;

fn main() {
    println!("Hello, world!");
    lexer::lexer::lex_stuff();
}

I've attempted to resolve this by changing the statement to use lexer as well as use self::lexer and adding extern crate lexer (which obviously doesn't work, but what the heck, figured I'd try it). However, none of these have worked.

What can I do to resolve the no 'lexer' in the root error?

like image 778
RPiAwesomeness Avatar asked Dec 28 '18 04:12

RPiAwesomeness


1 Answers

You still need to declare that main.rs contains a module:

mod lexer; // This, not `use`    

fn main() {
    println!("Hello, world!");
    lexer::lexer::lex_stuff();
}

Please take the time to re-read The Rust Programming Language, specifically the section about Separating Modules into Different Files.

like image 84
Shepmaster Avatar answered Nov 04 '22 09:11

Shepmaster