A similar path exists when using the rand library

I am using the ring library to provide encryption functions,but I need to generate some random numbers unrelated to encryption, and am unable to use the rand crate due to this error.

 use rand::Rng;
  |     ^^^^ help: a similar path exists: `ring::rand`

How can I fix this?

did you add rand into your Cargo.toml?

cargo add rand

Here is my Cargo.toml

[package]
name = "fallout_terminal"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ring = "0.16.20"
rand = "0.8.4"

what was the full error message then? is there's a name clash, i.e. did you import other module named rand? if you have other rand symbol in scope, try add a :: prefix to the crate name, like this:

use ::rand::Rng;
1 Like

Added a prefix to the crate name, that didn't work. Here's the full error I got when running cargo build.

error[E0432]: unresolved import `rand`
 --> src\main.rs:3:7
  |
3 | use ::rand::Rng;
  |       ^^^^ help: a similar path exists: `ring::rand`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `fallout_terminal` due to previous error

The error message is about a package named terminal_emulator, but the Cargo.toml you posted earlier has a package name of fallout_terminal.

If you have two packages involved, then you need to put rand in the [dependencies] of terminal_emulator. If something else is going on, please post more information, like the file tree of your entire project.

My mistake,the package is called fallout_terminal. Edited my previous response to reflect that.

Don't edit error messages — that hides important clues. Tell us why it was the way it was. Did you rename the package between this and your earlier post?

1 Like

I forgot that I renamed the package, yes. It was an attempt to see if that would effect cargo throwing the similar path error. I reverted it because that didn't work.

it's very strange. the external crate must be renamed or something, but I have no clue. the following are just my best guess:

  • check the output of cargo tree (or cargo metadata which is more verbose), is rand in the dependency tree?
  • do you have a workspace? if so, check Cargo.toml of the workspace;
  • run cargo clean; then cargo build -vv; is rand being built as a dependency?
  • check your Cargo.lock file, is anything suspicious there?
  • run cargo update and build again.

Here is what I've gotten. I ran cargo tree
As you can see,rand isn't in the deps tree for ring

cargo tree
fallout_terminal v0.1.0 (C:\Users\shred\Documents\GitHub\FalloutTerminal\Fallout-Terminal)
└── ring v0.16.20
    ├── spin v0.5.2
    ├── untrusted v0.7.1
    └── winapi v0.3.9
    [build-dependencies]
    └── cc v1.0.79

I don't have a workspace set up for this project.

cargo build -vv
showed that rand is not being built as a dependency.

My Cargo.lock file looks standard,but it's at this pastebin URL if you'd like to have a look.

I ran cargo update and built,and still ran into the same error.

your Cargo.lock file doesn't agree with your Cargo.toml manifest. your package fallout_terminal has ring as one only dependency, but the Cargo.toml you posted before says you have two dependencies: rand and ring.

[[package]]
name = "fallout_terminal"
version = "0.1.0"
dependencies = [
"ring",
]

I built the project again after trying to clean the workspace again and it works now. I'm completely unclear as to why this happened.

it might be possible that cargo is reading a "stale" copy of Cargo.toml, for unknown reasons. is the filesystem mounted or mapped through network? does your dev environment have some transparent caching mechanism? things like such.

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.