Rust can't find crate
Asked Answered
T

5

46

I'm trying to create a module in Rust and then use it from a different file. This is my file structure:

matthias@X1:~/projects/bitter-oyster$ tree
.
├── Cargo.lock
├── Cargo.toml
├── Readme.md
├── src
│   ├── liblib.rlib
│   ├── lib.rs
│   ├── main.rs
│   ├── main.rs~
│   └── plot
│       ├── line.rs
│       └── mod.rs
└── target
    └── debug
        ├── bitter_oyster.d
        ├── build
        ├── deps
        ├── examples
        ├── libbitter_oyster.rlib
        └── native

8 directories, 11 files

This is Cargo.toml:

[package]
name = "bitter-oyster"
version = "0.1.0"
authors = ["matthias"]

[dependencies]

This is main.rs:

extern crate plot;

fn main() {
    println!("----");
    plot::line::test();
}

This is lib.rs:

mod plot;

this is plot/mod.rs

mod line;

and this is plot/line.rs

pub fn test(){
    println!("Here line");
}

When I try to compile my program using: cargo run I get:

   Compiling bitter-oyster v0.1.0 (file:///home/matthias/projects/bitter-oyster)
/home/matthias/projects/bitter-oyster/src/main.rs:1:1: 1:19 error: can't find crate for `plot` [E0463]
/home/matthias/projects/bitter-oyster/src/main.rs:1 extern crate plot;

How do I compile my program? As far as I can tell from online documentations this should work, but it doesn't.

Timekeeper answered 25/12, 2015 at 15:15 Comment(0)
R
24

You have the following problems:

  1. you have to use extern crate bitter_oyster; in main.rs, because the produced binary uses your crate, the binary is not a part of it.

  2. Also, call bitter_oyster::plot::line::test(); in main.rs instead of plot::line::test();. plot is a module in the bitter_oyster crate, such as line. You are referring to the test function with its fully qualified name.

  3. Make sure, that every module is exported in the fully qualified name. You can make a module public with the pub keyword, like pub mod plot;

You can find more information about Rust's module system here: https://doc.rust-lang.org/book/crates-and-modules.html

A working copy of your module structure is as follows:

src/main.rs:

extern crate bitter_oyster;

fn main() {
    println!("----");
    bitter_oyster::plot::line::test();
}

src/lib.rs:

pub mod plot;

src/plot/mod.rs:

pub mod line;

src/plot/line.rs :

pub fn test(){
    println!("Here line");
}
Retrusion answered 25/12, 2015 at 16:3 Comment(0)
U
34

To add to the given answers, a library compiled as a cdylib (docs) can generate this error when you try to reference it in another project. I solved it by separating the code I wished to reuse in a regular lib project.

Urfa answered 10/4, 2018 at 21:17 Comment(1)
This is the case for example when using PY03 which required cdylib. To fix this I set crate-type = ["cdylib", "lib"] which means the library worked for both python compiling and using the crate in other rust libraries.Cabob
C
29

If you see this error:

error[E0463]: can't find crate for `PACKAGE`
  |
1 | extern crate PACKAGE;
  | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

it could be that you haven't added the desired crate to the dependencies list in your Cargo.toml:

[dependencies]
PACKAGE = "1.2.3"

See specifying dependencies in the Cargo docs.

Conti answered 28/12, 2016 at 0:14 Comment(3)
Note: googling this error sends you here, so I hope this helps someone else!Conti
How does one determine which version of a crate to specify? Is there always a place to look for release notes that would give a clue?Selene
@JamesJones usually what it says on crates.io will be what you want (the latest) crates.io/crates/serde, you can also see dependencies etc.Conti
R
24

You have the following problems:

  1. you have to use extern crate bitter_oyster; in main.rs, because the produced binary uses your crate, the binary is not a part of it.

  2. Also, call bitter_oyster::plot::line::test(); in main.rs instead of plot::line::test();. plot is a module in the bitter_oyster crate, such as line. You are referring to the test function with its fully qualified name.

  3. Make sure, that every module is exported in the fully qualified name. You can make a module public with the pub keyword, like pub mod plot;

You can find more information about Rust's module system here: https://doc.rust-lang.org/book/crates-and-modules.html

A working copy of your module structure is as follows:

src/main.rs:

extern crate bitter_oyster;

fn main() {
    println!("----");
    bitter_oyster::plot::line::test();
}

src/lib.rs:

pub mod plot;

src/plot/mod.rs:

pub mod line;

src/plot/line.rs :

pub fn test(){
    println!("Here line");
}
Retrusion answered 25/12, 2015 at 16:3 Comment(0)
M
1

I got this issue when I had imported my crate in [dev-dependencies] instead of [dependencies]

Midday answered 12/6, 2022 at 18:28 Comment(0)
F
0

This can also happen when you don't enable certain "feature flags" for specific crates. Unfortunately, these feature flags can sometimes be undocumented. When feature flags are missing when required, they show the same error ("Can't find crate")

I was using diesel, and was trying to use BigInteger:

Wrong

diesel = { version = "2.0.3", features = ["postgres", "chrono", "r2d2", "serde_json", "biginteger"] }

Correct:

diesel = { version = "2.0.3", features = ["postgres", "chrono", "r2d2", "serde_json", "numeric"] }
Fugacity answered 31/1, 2023 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.