0

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.

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

why the diesel official docs could do it like this?diesel.rs/guides/getting-started @Carl Gronvald his project is diesel_demo and create extern diesel_demo.
His library crate is called diesel_demo, but everything inside the bin folder is in fact not directly a part of the library crate. It is instead a separate binary crate, that can thus refer to the library crate. If you, instead of making a file called main.rs in the root, create a file inside a bin folder, and run cargo build --bin, I think it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.