[SOLVED] Trouble Resolving Import of `num` Consistently in Different Projects

I have a real hankering to use some BigInt and BigUInt, but I'm getting some odd behavior when trying to import num as a dependency in a larger library.

My module starts like this:

extern crate core;
extern crate num;

use std::io;

use num::bigint;

I made sure to include num as a dependency as well.

[package]
name = "name"
version = "0.1.0"
authors = ["authors"]

[[bin]]
name = "main"
doc = false

[dependencies]
num = "*"

...and the error I'm receiving in console is:

../blah.rs:4:5: 4:8 error: unresolved import `num::bigint`. Did you mean `self::num`? [E0432]
../blah.rs:4 use num::bigint;
                 ^~~

Having declared num as a dependency, this was odd, so I made another project just to try what I believe to be the same thing, to see if the issue was exclusive to my current project.

I spun up another cargo project and wrote out the import in the same manner:

extern crate num;

use num::bigint;

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

...with .toml:

[package]
name = "num_test"
version = "0.1.0"
authors = ["authors"]

[dependencies]
num = "*"

...and it builds with no errors!

I'm at a slight loss here, but I'm pretty sure I could have overlooked something since I'm pretty new to Rust. I've also manually deleted num and re-built a couple times to no avail. Has anyone else had this problem?

Is this a root module of a crate? You should not put extern crate declarations in every module. extern crate should be specified once in the top most module, and all children modules will be able to use it because in use num::bigint the path num::bigint is implicitly fully qualified (that is, use foo is the same as use ::foo).

2 Likes

That did it!

Yeah, in hindsight that does seem the practical place to put imports. I'm using this for cataloging algorithm problems I solve online, and every submission has me placing those at the top. I didn't think to refactor I was so used to them being up there--my mistake.

Thank you for the help!

There is a huge difference between extern crate and use declaration (which took me about a week to figure out when I was starting with Rust). I've tried to explain it with more details here: Cargo test internal packages - #8 by matklad

1 Like