PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

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

 August 24, 2022     module, rust, rust-2018     No comments   

Issue

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?


Solution

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.



Answered By - Shepmaster
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing