Quick Rust snippet: if you've got a module which requires an external crate, but then claims that it can't find the crate name in your use
statement, then make sure the external declaration is in your main.rs
or lib.rs
file, not the module file.
For instance:
// numbers.rs
extern crate rand;
// this causes an error!
// use rand::Rng;
// have to use self::rand
use self::rand::Rng;
pub fn pick_a_number() -> i32 {
return 12;
}
throws an error, saying rand
isn't recognised. Changing this to use self::rand:Rng;
does work, but the real fix is to move the extern crate rand
line to the main.rs
file:
// main.rs
// add crate declaration here
extern crate rand;
mod number;
fn main() {
// do something
}
// number.rs
// remove the crate declaration
// extern crate rand;
// now this works!
use rand::Rng;
pub fn pick_a_number() -> i32 {
return 12;
}
This is similar to having to declare the mod number
in main.rs
Basically, always put extern crate something;
in your main.rs
file, not the sub-module file.