Cargo rand compilation errors

This is a really basic question so please bear with me.

My issue is that cargo update registry downloads the latest version of rand and runs into error. i want it to ignore the latest version and download/compile the one i mentioned.

Details
I have a rust code which uses rand so i added rand="0.4.3" as a dependency. when the registry updates, randv0.5.5(latest) is automatically downloaded and it also runs into "break loop" error which was stabilized a while ago. I am not sure how to suppress this error or make it not install the latest version.

Cargo.toml

[package] 
name = "hello-world" 
version = "0.0.0" 
authors = [""]

[dependencies] 
time = ">=0.1.0" 
rand = "=0.4.3" 
rustc-serialize = "0.3"
histogram = "*"

I get this error. The repository is not cloned locally so i cannot apply the patch rust-lang/rust#37339.

Compiling rand v0.5.5
error: break with a value is experimental (see issue #37339)
--> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.5.5/src/distributions/uniform.rs:674:25
|
674 | break d;
| ^^^^^^^
|
= help: add #![feature(loop_break_value)] to the crate attributes to enable

error: pub(restricted) syntax is experimental (see issue #32409)
--> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.5.5/src/distributions/float.rs:71:5
|
71 | pub(crate) trait IntoFloat {
| ^^^^^
|
= help: add #![feature(pub_restricted)] to the crate attributes to enable

error: pub(restricted) syntax is experimental (see issue #32409)
--> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.5.5/src/rngs/mod.rs:174:27
|
174 | #[cfg(feature="std")] pub(crate) mod thread;
| ^^^^^
|
= help: add #![feature(pub_restricted)] to the crate attributes to enable

error: aborting due to 3 previous errors

error: Could not compile rand.

What am i doing wrong? What is the right way to go about it?

Are you perhaps using a very old Rust compiler? What does this command output?

 rustc -V

yes i am. that is why i am trying to make this install only my specified build. It is a requirement to use this rust version. cargo 0.19.0-nightly (c995e9e 2017-03-17)

Ok. So one of your dependencies or transitive dependencies is pulling in rand 0.5.5. You'll either need to find a version of your dependencies that doesn't transitively depend on rand 0.5.5 or use a [patch] section in your Cargo.toml file to replace that dependency. You can read more about this here.

thanks for your help. i am a beginner here. I am not sure how i can figure out which dependency is pulling rand. i tried removing each of them and adding them back one by one to narrow down the possibilities but even with all the dependencies removed, rand is still pulled.

How can figure out the dependency?

cargo tree is handy for this kind of question -- this is an external command, cargo install cargo-tree

Otherwise, you can dig into your Cargo.lock manually to see where that rand 0.5 is used.

1 Like

I believe it's rustc-serialize that's pulling it in as a dev-dependency since it specifies rand = ^0.3 as the semver.

Also note that rustc-serialize is deprecated and you likely want to transition to serde.

You were totally right. i found the dependency. the url which needs to be patched is this

[patch.'rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)']
rand = { git = "https://github.com/rust-random/rand", rev = "7d09b3606811300741dc1a44ce6a27ea846e971c" }

is this how we patch?
or

[patch.crates-io]
rand = { git = "https://github.com/rust-random/rand", rev = "7d09b3606811300741dc1a44ce6a27ea846e971c" }

Both are not working.

yes i had to remove it

BTW, you'll have pretty bad experience if you use anything but the latest stable compiler. The Rust ecosystem generally doesn't support old versions of the compiler.

The compiler is designed to be backwards compatible and very easy to painlessly upgrade (e.g. via rustup), so consider using always the latest stable Rust version.

1 Like

You are right kornel but the problem is that my code is not compatible with the latest build. I get errors like these.

Compiling e2d2 v0.1.0 (file:///home/mycode/NetBricks/framework)
error[E0432]: unresolved import alloc::heap
--> framework/src/allocators/cache_aligned.rs:1:12
|
1 | use alloc::heap::{allocate, deallocate};
| ^^^^ Could not find heap in alloc

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:2:1
|
2 | #![feature(asm)]
| ^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:3:1
|
3 | #![feature(log_syntax)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:4:1
|
4 | #![feature(box_syntax)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:5:1
|
5 | #![feature(specialization)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:6:1
|
6 | #![feature(associated_consts)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:7:1
|
7 | #![feature(slice_concat_ext)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:8:1
|
8 | #![feature(fnbox)]
| ^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:9:1
|
9 | #![feature(alloc)]
| ^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:10:1
|
10 | #![feature(heap_api)]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:11:1
|
11 | #![feature(unique)]
| ^^^^^^^^^^^^^^^^^^^

error[E0554]: #![feature] may not be used on the stable release channel
--> framework/src/lib.rs:14:1
|
14 | #![feature(integer_atomics)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors

Some errors occurred: E0432, E0554.
For more information about an error, try rustc --explain E0432.
error: Could not compile e2d2.

Oh, you're using nightly features. These have no guarantee of stability and will cause some code churn.

Still, I'd recommend upgrading the nightly compiler from time to time. You can't jump from old nightly to new stable, but you could update old nightly to new nightly.

You will have to keep fixing the unstable APIs that have changed — that's the price of nighty features and there's no way around it. If you don't, you're stuck forever with a dead unsupported version of Rust.

yes that's my problem precisely. Tried alot of things. I feel that i am almost done. Only if i got get overiding dependencies and sub-dependencies right.
Rand v0.5.5 is pulled by twox-hash. if i get an old twox-hash repo locally and replace it like this. should it work?

[dependencies]
twox-hash = "1.1.1"

[replace]
"twox-hash:1.1.1" = {path="twox-hash-old"}

Don't use [replace], at all. It's a dead feature full of problems. It has been replaced by patch. In your case [patch.crates-io].

[patch] was introduced in rust 1.21 have rust 1.17

Oh, it's going to be so much pain. The old [replace] requires the replaced dependency to have the same version. You could try putting fake version in Cargo.toml of the replaced twox-hash-old.

I suspect it'll be easier to make your code compatible with a newer Rust, than to try to downgrade everything else using a busted Rust version that couldn't downgrade dependencies.

1 Like

You might be able to just downgrade: cargo update -p twox-hash --precise 1.1.0

1 Like

It's working now. Thanks alot.