Rust can't find crate

后端 未结 3 1512
误落风尘
误落风尘 2021-01-01 10:52

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
.
├── Car         


        
3条回答
  •  醉酒成梦
    2021-01-01 11:10

    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");
    }
    

提交回复
热议问题