Rust can not find the box

I am trying to create a module in Rust and then use it from another 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 docs, this should work, but it is not.

+7
rust rust-crates
source share
2 answers

You have the following issues:

  • you need to use extern crate bitter_oyster; in main.rs , because the resulting binary uses your box, the binary is not part of it.

  • Alternatively, call bitter_oyster::plot::line::test(); in main.rs instead of plot::line::test(); . plot is a module in the bitter_oyster box, for example line . You mean the test function with its full name.

  • Make sure that each module is exported with the full name. You can make the module public with the pub keyword, for example pub mod plot;

Further information on the Rust module system can be found here: https://doc.rust-lang.org/book/crates-and-modules.html

The working structure 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; 

CSI / plot / mod.rs:

 pub mod line; 

src / plot / line.rs:

 pub fn test(){ println!("Here line"); } 
+11
source share

If you see this error:

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

it is possible that you did not add the desired box to the list of dependencies in Cargo.toml :

 [dependencies] PACKAGE = "1.2.3" 

See specifying dependencies in Cargo documents .

+4
source share

All Articles