How to include files from same directory in a module using Cargo/Rust?

前端 未结 1 1573
一整个雨季
一整个雨季 2020-12-09 10:25

I have a Cargo project consisting of three files in the same directory: main.rs, mod1.rs and mod2.rs.

I want to import funct

相关标签:
1条回答
  • 2020-12-09 10:35

    All of your top level module declarations should go in main.rs, like so:

    mod mod1;
    mod mod2;
    
    fn main() {
        println!("Hello, world!");
        mod1::mod1fn();
    }
    

    You can then use crate::mod2 inside mod1:

    use crate::mod2;
    
    pub fn mod1fn() {
        println!("1");
        mod2::mod2fn();
    }
    

    I'd recommend reading the chapter on modules in the new version of the Rust book if you haven't already - they can be a little confusing for people who are new to the language.

    0 讨论(0)
提交回复
热议问题