Mystery -- Dependencies not imported

Well, we had a three-day hiatus from the internet here in the Northwest due to an ice storm. Really messed with my coding, but last night we got it back, so this morning I pulled up my main project to get started and for some reason the compiler isn't importing my dependencies. It's not only refusing to recognize the dependency crate (there's only one right now) but it doesn't like my local lib.rs file that is saved in the same directory. It was working before the ice fell, but not now. Here's what I've tried:

  1. Inserted crate:: between use and the crate name. Like this:
use crate::num_traits::pow;
use crate::qbnk_rebuild::{TypeWrapper, TypeWrapper::*}; // This is for the local lib.rs.  

No change.

  1. Went to the terminal to run cargo build just in case it needed to re-import the crates. No change. Still didn't work.

  2. Checked my cargo.toml file to make sure the crate was imported. It was fine, but since num_traits is now up to version "0.2.17" I upgraded it from "0.2.0". Then ran cargo build again and still no go.

So, I'm at a standstill here this morning. Obviously, I'm missing something, probably something simple that should be obvious to me. :nerd_face: Any thoughts?

The crate:: prefix refers to your own crate, so it would only let you find modules and other items from the root file (lib.rs or main.rs, typically), including re-exports, etc. Other crates are prefixed with just their names.

Could you post the errors you get when you compile without crate:: on dependency paths? That would help a lot.

Edit: you could try running cargo clean, in case there's an issue with caches from the outage.

Thanks for getting back to me. Here's the error message:

error[E0432]: unresolved import `num_traits`
 --> src/lib.rs:4:5
  |
4 | use num_traits::pow;
  |     ^^^^^^^^^^ maybe a missing crate `num_traits`?
  |
  = help: consider adding `extern crate num_traits` to use the `num_traits` crate

For more information about this error, try `rustc --explain E0432`.
error: could not compile `qbnk_rebuild` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...

Ran cargo clean and it did find some stuff:

     Removed 6343 files, 3.7GiB total

Followed up with cargo build but it didn't solve the problem.

Are you perhaps missing the edition field in your Cargo.toml? With no edition set Cargo defaults to 2015 which requires the extern crate statements the error message is suggesting. If it's missing, try adding edition = "2021" there.

4 Likes

Alright, but it's good to rule it out as a factor whenever there are odd issues like these.

And @Heliozoa solved the problem. I did mess around with my cargo.toml file while I was waiting around for the internet to come back up. The edition = "2021" line solved the problem.

Thanks!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.