error: can't find crate - rust

I'm trying to use this library.
But, cargo build says this:
Compiling test v0.1.0 (file:///C:/path/to/project/test)
src\main.rs:1:1: 1:28 error: can't find crate for `jvm_assembler` [E0463]
src\main.rs:1 extern crate jvm_assembler;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `test`.
To learn more, run the command again with --verbose.
My Cargo.toml is this:
[package]
name = "test"
version = "0.1.0"
authors = ["yomizu_rai"]
[dependencies]
jvm-assembler = "*"
src/main.rs is this, and there are no other sourcefiles.
extern crate jvm_assembler;
use jvm_assembler::*;
fn main() {}
I think my Cargo.toml is not wrong, and src/main.rs has no room for mistake.
Why can not rustc find jvm-assembler?
How do I resolve?

Cargo can only find crates by name if they are on crates.io. In your case you need to specify the git URL, see the section on dependencies in the Cargo documentation.

Related

Unable to use rust crates

I'm new to rust. I'm following a getting started tutorial that imports the crate random-number but when running the code I'm getting the error can't find crate for 'random_number'. What am I doing wrong?
~/Cargo.toml:
[package]
name = "test"
version = "0.0.1"
edition = "2021"
[dependencies]
random-number = "0.1.8"
~/src/main.rs:
extern crate random_number;
use random_number::random;
fn main() {
let num: i8 = random!(..);
println!("{}", num);
}
rustc is not meant to be used directly. It is the compiler that can compile a .rs file, but it doesn't have any dependency manager attached to it. So if you decide to use rustc directly, you need to manage your dependencies manually.
cargo is the official tool to compile Rust projects. It internally uses rustc, but additionally manages the project's dependencies that are specified in Cargo.toml.
cargo build --release && ./target/release/<project_name>
or the short form:
cargo run --release

"unresolved import thiserror" when expanding the code with "rustc -Zunpretty=expanded"

My Cargo.toml
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
thiserror = { version = "1.0.30", default-features = false }
My rustc --version
rustc 1.59.0-nightly (cfa3fe5af 2021-12-31)
My package structure
src/
error.rs
lib.rs
main.rs
My lib.rs
pub mod error;
My error.rs
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DataStoreError {
#[error("the data for key `{0}` is not available")]
Redaction(String),
#[error("invalid header (expected {expected:?}, found {found:?})")]
InvalidHeader { expected: String, found: String },
#[error("unknown data store error")]
Unknown,
}
I am trying to expand the error.rs to understand what #[derive(Error, Debug)] does to the code. However, running
rustc -Zunpretty=expanded src/error.rs
results in 5 errors, the first of which is unresolved import thiserror, which makes me think I need to change how I invoke the compiler, perhaps with a full module path to error?
rustc does not know about the crates in Cargo.toml. It does not know about this file at all. It's Cargo that uses it, and passes things as flags to rustc.
Don't do this work manually. It'll require replicating all of the work Cargo does.
Instead, Cargo has a command for running rustc commands: cargo rustc. You pass the arguments to rustc after the --:
cargo rustc -- -Zunpretty=expanded
However, there is not a flag to filter the results. Passing a filename to rustc just causes it to consider this file as the entry point, and it will not work with Cargo since it passes its own file as the entry point: lib.rs or main.rs.
However, as stated in the comment from #Cerberus, it's preferred to use cargo expand. Note that it does not take a filename but a path, so to expand error it will look like:
cargo expand error
And to expand DataStoreError:
cargo expand error::DataStoreError

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)

Build script is unable to find crate that is listed in the [dependencies] section

Minimal reproducible example:
build.rs
extern crate pkg_config;
fn main() {}
Cargo.toml
[dependencies]
pkg-config = "0.3"
Running cargo check or cargo build on this example results in an error: error[E0463]: can't find crate for pkg_config.
Cargo downloaded the crate just fine and I've verified that I have pkg-config installed. Is there some rule I'm missing about build.rs dependencies? Is there some other configuration I need specifically for pkg-config?
Instead of pkg-config under [dependencies], you need to list it under [build-dependencies] in your Cargo.toml file. Cargo differentiates between the two. The first is for the app or library, while the [build-dependencies] are for the build.rs script.

How to import a crate dependency when the library name is different from the package name?

I have a crate that is imported straight off of GitHub, as per Cargo's documentation:
[dependencies]
libfoo = { git = "ssh://git#github.com/me/libfoo", branch = "dev" }
[lib]
path = "src/rust/lib.rs"
name = "myprj"
crate-type = ["cdylib"]
Running cargo build works fine here, Cargo fetches libfoo and builds it in the ~/.cargo directory. When I try to use (import) it in lib.rs:
extern crate libfoo; //also tried foo
Cargo chokes:
error[E0463]: can't find crate for `libfoo`
--> src/rust/lib.rs:1:1
|
1 | extern crate libfoo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
Interestingly, IntelliJ's Rust plugin does find the crate when I click on it in lib.rs – it navigates to the downloaded source in ~/.cargo...
In the dependency libfoo, the lib section of the Cargo.toml file is specified as:
[package]
name = "libfoo"
[lib]
name = "foo"
crate-type = ["cdylib"]
I have tried all permutations of libfoo and foo to see if Cargo is getting confused between the lib name and the package/directory name.
It also fails if I specify a local path to the dependency. (Cargo compiles the dependency but then claims not to find it when it is declared/imported in lib.rs.)
[dependencies]
libfoo = { path = "/Users/me/dev/libfoo" }
If I include a crate from git or the file system that has the same [lib] name as the [package] name, it works fine. So it appears the problem is with crates that have a have a library ([lib]) name that is different from the package ([package]) name.
If I remove the [lib] section from the dependency's Cargo.toml file, it works.
Update: if crate-type = ["cdylib"] is removed from libfoo, this works with foo imported. If that is there, I get the same error with extern crate foo;.
Cargo is interested in package names when it comes to dependencies, while the compiler (rustc) is interested in library names when it comes to loading their metadata and linking with them.
Let's take a look again at this Cargo.toml excerpt:
[package]
name = "libfoo"
[lib]
name = "foo"
Here, the package name is libfoo and the library name is foo.
When you want to declare a dependency on libfoo in your project, you need to write the package name (libfoo) in the [dependencies] table. For example:
[dependencies]
libfoo = { git = "ssh://git#github.com/me/libfoo", branch = "dev" }
This is what you already have, and it's correct.
However, when you want to import the library in your crate, you need to write the library name in the extern crate item, i.e.
extern crate foo;
How did I figure this out? First, I wrote libfoo in both Cargo.toml and the extern crate item, as you described. When I ran cargo build, I noticed that libfoo was built successfully, indicating that Cargo correctly resolved the dependency. But I also noticed that the compiler couldn't find libfoo, as you experienced.
I then inspected the command line passed to rustc by running cargo build --verbose. This is what I saw (irrelevant parts omitted):
Running `rustc [...] --extern foo=/[path]/target/debug/deps/libfoo-5cf876c5c8ac1bfb.rlib`
The --extern name=path argument tells rustc that the crate named name is located in path. The name here is foo, so we must write extern crate foo; in the code to reference it.

Resources