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

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.

Related

can't find crate when using extern command

I am learning rust, now I tried to use Diesel to do some operation to database. I write the main.rs like this:
#[macro_use]
extern crate reddwarf_music;
fn main(){
}
then compile the project using command cargo build, shows error like this:
~/Documents/GitHub/reddwarf_music on  develop! ⌚ 14:50:50
$ cargo build ‹ruby-2.7.2›
Compiling reddwarf_music v0.1.0 (/Users/dolphin/Documents/GitHub/reddwarf_music)
error[E0463]: can't find crate for `reddwarf_music`
--> src/main.rs:3:1
|
3 | extern crate reddwarf_music;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: could not compile `reddwarf_music`
To learn more, run the command again with --verbose.
(base)
and this is my Cargo.toml:
[package]
name = "reddwarf_music"
version = "0.1.0"
edition = "2018"
[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
rand = "0.8.4"
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
reqwest = "0.11.4"
# database
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
am I doing the right way? the next step I want to use the mod like this:
use reddwarf_music::schema::posts::dsl::*;
I am follow the docs step by step from here to using diesel. what should I do to make it work as expect? the diesel official docs use it like this way. In my project, it did not work.
As far as I can see, the crate you are trying to compile is called reddwarf_music. extern crate tries to include a crate into the crate you're currently compiling. You're in effect trying to include the crate reddwarf_music into itself in order to use proc_macros from reddwarf_music in reddwarf_music. This is unfortunately not possible.
What the official Diesel documentation does is slightly different. Their library crate is called diesel_demo, but the code run when running cargo run --bin is actually the code inside the folder bin, which is not directly a part of the library crate, and instead part of a separate binary crate. If you instead of the file main.rs in the root, create a file inside a bin folder and run cargo build --bin, I think it should work.
(This has been edited to more directly answer the question)

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.

Rust Nickel Hello World tutorial throwing dependency error when run

I am learning Rust, and saw a post on http://reddit.com/r/rust yesterday for Nickel. As a Node.js developer in my free time, I was interested in checking this out.
I downloaded the Rust 1.0.0-beta DMG from http://rust-lang.org.
I followed the Hello World tutorial precisely, and when I execute cargo run in my Terminal, I am receiving the following error:
Robs-MacBook-Pro:nickel-demo rob$ cargo run
Compiling nickel-demo v0.0.1 (file:///Users/rob/Workbench/nickel-demo)
src/main.rs:4:1: 4:21 error: an external crate named `nickel` has already been imported into this module [E0259]
src/main.rs:4 extern crate nickel;
^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `nickel-demo`.
The Hello World main.rs file for the Nickel Demo looks like this:
extern crate nickel;
#[macro_use] extern crate nickel_macros;
extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
"Hello world!"
}
});
server.listen("127.0.0.1:6767");
}
As I was typing this code into my editor, I specifically thought it was weird that I was declaring extern crate nickel; twice in the file. After receiving the error I refactored the code to this:
extern crate nickel;
#[macro_use] extern crate nickel_macros;
use nickel::Nickel;
...
And I get this error:
Robs-MacBook-Pro:nickel-demo rob$ cargo run
Compiling nickel-demo v0.0.1 (file:///Users/rob/Workbench/nickel-demo)
Running `target/debug/nickel-demo`
Listening on http://127.0.0.1:6767
Ctrl-C to shutdown server
thread '<main>' panicked at 'arithmetic operation overflowed', /Users/rob/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.3.11/src/server/mod.rs:90
An unknown error occurred
The arithmetic operation overflowed bug seems to be an upstream issue. It also affects hyper which nickel depends on.
See https://github.com/seanmonstar/num_cpus/issues/2
As a temporary workaround use cargo build --release to prevent the checks altogether.

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.

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

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"

Resources