Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unresolved import -- maybe a missing extern" When extern declaration exists

I have a small project which built with no issues when it was all in one big .rs file. I wanted to make it easier to work with, so I broke it up into modules, and the project is now structured like this:

├── GameState
│   ├── ballstate.rs
│   ├── collidable.rs
│   ├── gamestate.rs
│   ├── mod.rs
│   └── playerstate.rs
├── lib.rs
└── main.rs

In ballstate.rs, I need to use the rand crate. Here's an abbreviated version of the file:

extern crate rand;

pub struct BallState {
    dir: Point,         
    frame: BoundingBox  
}                     

impl BallState {
    fn update_dir(&mut self) {
        use rand::*;                                                                                                                                                                    
        let mut rng = rand::thread_rng();                                                                      
        self.dir.x = if rng.gen() { Direction::Forwards.as_float() } else { Direction::Backwards.as_float()  };
        self.dir.y = if rng.gen()  { Direction::Forwards.as_float() } else { Direction::Backwards.as_float() };
    }                                                                                                        
}

However, when I run cargo build from the top level directory, I get the following error:

GameState/ballstate.rs:42:9: 42:13 error: unresolved import rand::*. Maybe a missing extern crate rand?

When I just had the extern crate declaration in my main.rs file, this worked. What's changed now that it's in a separate module?

like image 585
Ben Pious Avatar asked Nov 26 '15 23:11

Ben Pious


2 Answers

To quote from the Crates and Modules chapter of the Rust book:

[...] use declarations are absolute paths, starting from your crate root. self makes that path relative to your current place in the hierarchy instead.

The compiler is correct; there is no such thing as rand, because you've put it inside a module, so the correct path to it would be GameState::ballstate::rand, or self::rand from within the GameState::ballstate module.

You need to either move extern crate rand; to the root module or use self::rand within the GameState::ballstate module.

like image 104
DK. Avatar answered Oct 23 '22 05:10

DK.


You need to put the extern crate rand; line in you main.rs and/or lib.rs file. No need to put it in the other files.

Perhaps it is related to this bug.

like image 28
antoyo Avatar answered Oct 23 '22 04:10

antoyo