multiple matching crates for `time` but there're no versions specified - rust

I've imported a create time and I'm using another crate which also uses (imports) time, because of this I have an error:
/Users/alex/Documents/projects/rust/my_project/src/lib.rs:2:1: 2:19 error: multiple matching crates for `time`
/Users/alex/Documents/projects/rust/my_project/src/lib.rs:2 extern crate time;
^~~~~~~~~~~~~~~~~~
note: candidates:
note: path: /usr/local/lib/rustlib/x86_64-apple-darwin/lib/libtime-4e7c5e5c.dylib
note: path: /usr/local/lib/rustlib/x86_64-apple-darwin/lib/libtime-4e7c5e5c.rlib
note: crate name: time
note: path: /Users/alex/Documents/projects/rust/my_project/target/deps/libtime-8fdb58a7632ec071.rlib
note: crate name: time
/Users/alex/Documents/projects/rust/my_project/src/lib.rs:2:1: 2:19 error: can't find crate for `time`
/Users/alex/Documents/projects/rust/my_project/src/lib.rs:2 extern crate time;
^~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
If I comment out time I'll have an error as well that time isn't found. The error above doesn't even explain what the versions of time are. Which one should I delete from the disk?

The crate time has recently been move to an external repository, but you currently still have the old crate bundled with rustc, as it is currently kept as deprecated.
Thus you have two crates "libtime", and rustc doesn't know which take.
As you're using cargo, the fix is simple: add libtime in your dependencies by putting this in your Cargo.toml, cargo will tell rusctc which libtime it should use.
[dependencies.time]
git = "https://github.com/rust-lang/time"

Related

Is there a way to fix "maybe a missing crate [crate_name]"?

My Rust crate wont load when compiling/running my file
I have reinstalled Rust several times, continuously restarting and checking the Path variables (as well as the current version installed)
I have restarted my system various times, and I am also experiencing this issue on my desktop PC at home; I am currently running Windows 11 as of the time of this post
I have added several other "dependencies" to my Cargo.toml file to no avail, they still refuse to work and I receive the same errors each time I try to utilise it
I have seen that some features of the Rust crate importation system have been deprecated; such as the "extern crate {crate_name}", I still receive these messages from time to time even though I am using the most up to date "rustup" version
I have consulted the Rust discord server and the official Rust language forums with the same issue
I have installed "git" on my system as that appeared to be an issue when adding my desired crates to the Cargo.toml
I expected to be able to use these crates without any hassle after solving my issue on importing these crates within the toml file, but I am still with nothing to show for that expectation.
Below is the code I have fully tested and ran
I have researched the documentation for importing crates with dependencies in Rust,
my directory looks like this: file directory
I have added the "ncurses" to my MinGW package manager, as requested by the "cursive" page on "crates.io"
I also gain the error from the "rust-analyser" extension in VSC
analyser error message
I have the current version of Rust installed,
rustup -V
rustup 1.25.1 (bb60b1e89 2022-07-12)
My File Directory looks like this:
-->Application
---->src
------>main.rs
---->target
---->.gitignore
---->Cargo.lock
---->Cargo.toml
My issue occurs when I try the command "cargo build" within the VSC terminal- which to my knowledge as I am in the bin file, it should run just fine.
I am thrown this error,
c1: fatal error C1083: Cannot open source file: 'C:\Users\dean7\Desktop\main.rs\crates\cratess\target\debug\build\ncurses-
4eee2f93b40e2da2\out\chtype_size.c': Permission denied
--- stderr
cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
thread 'main' panicked at 'assertion failed: command.status().expect(\"compilation failed\").success()', C:\Users\{%USERPROFILE%}\.cargo\registry\src\github.com-1ecc6299db9ec823\ncurses-5.101.0\build.rs:105:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
My "Cargo.toml" file looks like this,
[package]
name = "cratess"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cursive = "0.20.0"
My "main.rs" file looks like this,
use cursive::{Cursive, CursiveExt};
fn main() {
let mut app = Cursive::new();
app.run();
println!("Hello, world!");
}
My error when I run the command, run my code is this,
error[E0432]: unresolved import `cursive`
--> tempCodeRunnerFile.rs:1:5
|
1 | use cursive::{Cursive, CursiveExt};
| ^^^^^^^ maybe a missing crate `cursive`?
|
= help: consider adding `extern crate cursive` to use the `cursive` crate
error: aborting due to previous error
For more information about this error, try `rustc --explain E0432`.
An additional warning is given when I run "cargo build" in my terminal (NOT IN CMD),
warning: build failed, waiting for other jobs to finish...

Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc?

My Cargo.toml includes this:
[dependencies]
chrono = "0.4"
And my code includes this:
extern crate chrono;
use chrono::{Duration, DateTime, Utc};
yet when I run my code, I get this error:
error[E0463]: can't find crate for `chrono`
--> src/lib.rs:1:1
|
1 | extern crate chrono;
| ^^^^^^^^^^^^^^^^^^^^ can't find crate
I am working on an Exercism exercise, so the way I am building/running the program is rustc src/lib.rs to test my solution. Is the problem because I'm not running rustc src/main.rs?
When you're directly running rustc, all that compiler knows is the command line arguments. It doesn't know anything about Cargo.toml, in particular, and so it doesn't know where to look for chrono library.
To use dependency management, you have to compile your project with Cargo - just use cargo build/cargo run/cargo test, and everything should be fine. See the Book for details.
If, however, you want (for some reason) use rustc directly, I'd advise to check first cargo anyway, by using cargo build --verbose. It will show all the commands that are invoked, allowing you to inspect the possible arguments to be defined manually.

Why is this unrelated crate breaking my project?

$ cargo build
Downloading pear_codegen v0.0.16
Compiling pear_codegen v0.0.16
Compiling ring v0.11.0
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> /.cargo/registry/src/github.com-1ecc6299db9ec823/pear_codegen-0.0.16/src/lib.rs:317:9
|
317 | ExprKind::Block(block) => {
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
error: aborting due to previous error
For more information about this error, try `rustc --explain E0023`.
error: Could not compile `pear_codegen`.
warning: build failed, waiting for other jobs to finish...
error: build failed
When I try to compile my project I get an error that a crate that isn't in my cargo.toml is broken. Why is this unrelated crate breaking my project?
You're using Rust nightly, and the crate in question used to depend (in version 0.0.16) on the perma-unstable API of the Rust compiler, which was changed by a nightly you updated to (or just installed).
The author appears to have recently rewritten the crate to avoid that dependency, which means further breakage should be avoided, starting with version 0.0.17 of the crate.
cargo update
fixed the issue for me
https://github.com/SergioBenitez/Pear/issues/7
thanks to reddit user /u/usernamedottxt

Unable to run a simple example -- "use of unstable library feature 'rustc_private': ....."

I'm trying to use simplelog.rs in my Rust application. The hello world example
#[macro_use]
extern crate log;
extern crate simplelog;
// ..........
CombinedLogger::init(vec![
TermLogger::new(LogLevelFilter::Warn, simplelog::Config::default()).unwrap(),
WriteLogger::new(LogLevelFilter::Info, simplelog::Config::default(), File::create("log.log").unwrap())]).unwrap();
results into this:
error: 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:9:1
|
9 | extern crate log;
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error
How to fix it?
From the discussion of #27812 it's still unclear what to do about it.
You're missing log = "version" entry in Cargo.toml [dependencies].
Because of that Cargo doesn't give Rust a log crate to load with extern crate log, and Rust keeps looking for it deeper, finding some internal one.

Unable to find symbols from extern crates included with `use`

I'm trying to use some Rust libraries from crates on Github. This is the first time I've tried to do this. The code, lifted from an "html" library example, begins like this:
mod interactive_test {
extern crate http;
extern crate url;
use std::os;
use std::str;
use url::Url;
use http::client::RequestWriter;
use http::method::Get;
use http::headers::HeaderEnum;
// ...
}
fn main() {}
Errors look like this:
error[E0432]: unresolved import `url::Url`
--> src/main.rs:7:9
|
7 | use url::Url;
| ^^^^^^^^ Did you mean `self::url`?
error[E0432]: unresolved import `http::client::RequestWriter`
--> src/main.rs:9:9
|
9 | use http::client::RequestWriter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
error[E0432]: unresolved import `http::method::Get`
--> src/main.rs:10:9
|
10 | use http::method::Get;
| ^^^^^^^^^^^^^^^^^ Did you mean `self::http::method`?
error[E0432]: unresolved import `http::headers::HeaderEnum`
--> src/main.rs:11:9
|
11 | use http::headers::HeaderEnum;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
The Cargo.toml file contains
[dependencies.http]
http = "https://github.com/chris-morgan/rust-http"
[dependencies.url]
url = "0.2.7"
and the HTTP and URL packages were found and fetched by cargo build earlier.
The extern crate http and extern crate url lines do not generate errors; the crates are being found by the compiler, but those crates don't seem to contain the expected symbols. If I add `extern crate foo", I get an error, so that is checked.
This is probably some problem with how Rust or Cargo search for libraries. Rust is installed in ~/local, not as root, done by setting the --prefix parameter during installation. That may have broken something, although Cargo should handle that. Basic stuff like "hello_world" works fine; bringing in external libraries does not.
I notice that cargo update doesn't cause a re-fetch of the http and url crates from Github. The documentation indicates that it should.
Versions:
Ubuntu 14.04 LTS.
rustc 0.13.0-nightly (96a3c7c6a 2014-12-23 22:21:10 +0000)
cargo 0.0.1-pre-nightly (e11c317 2014-12-21 20:43:45 +0000)
The compiler gave you the answer you need.
Your extern crate statements are inside a module, and use statements require absolute paths. That is, when you say use url::Url; inside the interactive_test module, what you are actually saying is "use url::Url which is defined in the root module", which it isn't.
What you need to do is prefix the path with self:: to tell it to look in the current module. You can also use super:: to access the parent module (if that ever comes up).
Personally, I get around this by putting all my extern crate statements in the root module, which also serves as a kind of program-wide list of external crates being used.

Resources