Multiple matching crates for lazy_static

Hi,

I’ve got a problem when I want to run the example code from here https://docs.rs/lazy_static/1.2.0/lazy_static/

My test project is this

test <- project folder
test\src <- src folder
test\src\main.rs <- file with the lazy_static example code from the docs
test\cargo.toml <- just some [package] info in there

Should I upload a zip file of it?

It’s a fresh project and I tried to help myself by using “cargo clean” from the project directory, but it doesn’t help. When I do cargo run, I will get this:

C:\Users\m\Desktop\test>cargo run
   Compiling test v0.1.0 (C:\Users\m\Desktop\test)
error[E0464]: multiple matching crates for `lazy_static`
 --> src\main.rs:2:1
  |
2 | extern crate lazy_static;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: candidates:
          crate `lazy_static`: \\?\C:\Users\m\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liblazy_static-37b5c91ecac26461.rlib
          crate `lazy_static`: \\?\C:\Users\m\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liblazy_static-5c816a868d205f19.rlib

error[E0463]: can't find crate for `lazy_static`
 --> src\main.rs:2:1
  |
2 | extern crate lazy_static;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to 2 previous errors

Some errors occurred: E0463, E0464.
For more information about an error, try `rustc --explain E0463`.
error: Could not compile `test`.

To learn more, run the command again with --verbose.

C:\Users\m\Desktop\test>pause
Press any key to continue . . .

It seems I have two of these rlib files for lazy_static?

Do you have any [dependency] sections in you Cargo.toml?

No, it’s just:

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

Here’s the zip if you need https://yadi.sk/d/20rnqUsfij8RJQ

I think what I might have done to cause it is using certain dependencies in earlier projects. Before I had tried “winapi” with features “winuser” and “windef”, “user32-sys” and “kernel32-sys”, and “gl-rs”. The rust installation is only two days old, so I hesitated to install it again. But that’s what I’ll try now.

In that .rustup lib folder there are two files, and seemingly they get installed right from the beginning.

Untitled

I’ve managed to go on compiling by renaming one of them to rlib_. In both cases the next compilation error is this:

C:\Users\m\Desktop\test>cargo run
   Compiling test v0.1.0 (C:\Users\m\Desktop\test)
error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)
 --> src\main.rs:2:1
  |
2 | extern crate lazy_static;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)
  --> src\main.rs:6:1
   |
6  | / lazy_static! {
7  | |     static ref HASHMAP: HashMap<u32, &'static str> = {
8  | |         let mut m = HashMap::new();
9  | |         m.insert(0, "foo");
...  |
15 | |     static ref NUMBER: u32 = times_two(21);
16 | | }
   | |_^
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
error: Could not compile `test`.

To learn more, run the command again with --verbose.

C:\Users\m\Desktop\test>pause
Press any key to continue . . .

I have to say, all I actually tried to do is convert C++ code to Rust in order to learn it. And there is this problematic thing in the C++ code that is a map of strings to strings, a
std::map<std::string, std::string> g_settings; // C++ code
that represents some global settings. So, all I wanted to do in Rust is actually have two functions that share a (global) string-to-string-map or anything like that to store things like so:
set_setting("favourite_red", "#ff1100") // Rust code
and using
get_setting("favourite_red") // Rust code.
I landed at the lazy_static example then, but anything would work for me.

Have you tried adding

[dependencies]
lazy_static = "1.2.0"

to your Cargo.toml?

The error message you posted tells you this:

did you mean to load this crate from crates.io via Cargo.toml instead? (see issue #27812)

2 Likes

Yeah, the problem is that the compiler is attempting to give you its private copy of lazy_static. Apparently at some point it pulled in two versions, and the error messages you’re getting are very confusing.

These compiler-private crates really need to be isolated better from the cargo build environment IMO.

Feel free to go ahead and open an issue on github.com/rust-lang/rust with the messages from the initial posting.

4 Likes

Thanks a lot, adding the dependency has fixed it. I’ll do it like this from now on.
(The compiler’s hint to do so comes only after renaming one of the private rlib files to rlib_.)