Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust can't find crate

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.

like image 617
Stein Avatar asked Dec 25 '15 15:12

Stein


People also ask

What is extern crate in Rust?

extern crate foo indicates that you want to link against an external library and brings the top-level crate name into scope (equivalent to use foo ). As of Rust 2018, in most cases you won't need to use extern crate anymore because Cargo informs the compiler about what crates are present. (

How do crates work in Rust?

A crate is the smallest amount of code that the Rust compiler considers at a time. Even if you run rustc rather than cargo and pass a single source code file (as we did all the way back in the “Writing and Running a Rust Program” section of Chapter 1), the compiler considers that file to be a crate.


1 Answers

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.

like image 109
Andy Hayden Avatar answered Oct 08 '22 02:10

Andy Hayden