Local crate in Rust
Asked Answered
C

1

6

I am currently learning Rust, and for that purpose I wanted to create my own crate and use it. However, Rust can't find this crate.

I have the following file structure:

├───minimal
│    ├───Cargo.toml
│   └───src
│       └───main.rs
└───util
    └───win
        ├───Cargo.toml
        └───src
            └───lib.rs

In folder minimal I have my main project. It looks like this:

Cargo.toml

[package]
name = "minimal"
version = "0.1.0"
[dependecies]
win = { path = "../util/win"}

main.rs

extern crate win; // ERROR: "Can't find crate for 'win' rustc(E0463)"
fn main() {
    println!("Hello, World!");
}

My library in folder win looks like this:

File Cargo.toml

[package]
name = "win"
version = "0.1.0"

File lib.rs

pub type TestType = String;

My first assumption was that I somehow had a mistake in specifying the path in the dependency of the Cargo.toml file. So I tried to wiggle it around a bit, but it does not seem to work.

Rust reports

can't find crate for 'win' rustc(E0463)

I feel like I am making a very basic error here, however, when looking at similar questions (e.g., How do I "use" or import a local Rust file?) I can't seem to find it.

Ctenoid answered 7/12, 2019 at 20:28 Comment(8)
Have you tried with an absolute path to see if that works?Vogue
I have tried it, yes - unfortunately it didn't work. Do I need to compile my library in a specific way for this to work?Ctenoid
Don't think so... I just replicated your setup and can't get the same error. Another thing to try could be to use the 2018 edition, though I doubt it will matter.Vogue
Just to check, you are using cargo to build/run this right? I.e. Running cargo run in the minimal directoryVogue
I use cargo run to run this, yes.I installed rust from here: rust-lang.org/tools/install and I run it from visual studio code in windows 10. (I get the same error if I use the cmd-prompt directly.) Strange that it works out of the box for you.Ctenoid
@Ctenoid It works for me too. No errors.Sectorial
Have you created any additional files except the four I use?Ctenoid
@Ctenoid No additional files. Mine looks just like yours.Sectorial
C
16

After having a good night's sleep and looking at this problem again, I have managed to find the error.
I used [dependecies] instead of [dependencies] in the Cargo.toml file.

On the one hand, I feel kind of dumb for this error, but on the other hand I now know that Cargo will not warn about unknown tags in the TOML file.

Ctenoid answered 8/12, 2019 at 9:4 Comment(1)
I like the addition of how you feel about the result. Refreshing and wholesome.Golly

© 2022 - 2025 — McMap. All rights reserved.