Turtle
Turtle

Reputation: 1389

Include Module Rust

My question is pretty similar to How to include module from another file from the same project?, in that I am trying to import a mod into my main.rs and use it, except my mod has a private and public function.

sys.rs

mod sys {
    fn read_num_lines(file: File, num_lines: i32) -> bool {
        //do bar with foo
    }
    pub fn get_cpu_stats() {
        //call read_num_lines
        //doo foo
    }
}

main.rs

mod sys;
fn main() {
    sys::get_cpu_stats();
}

I get the following build error:

unresolved name sys::get_cpu_stats

Since this is my first Rust project, I'm sure I'm doing something wrong, but am unsure as to what that something is.

Upvotes: 1

Views: 170

Answers (1)

Turtle
Turtle

Reputation: 1389

Change sys.rs to:

fn read_num_lines(file: File, num_lines: i32) -> bool {
    //do bar with foo
}
pub fn get_cpu_stats() {
    //call read_num_lines
    //doo foo
}

since the file sys.rs is already a module scope. I could have also written sys::sys::get_cpu_stats();

Thanks to June in IRC!

Upvotes: 1

Related Questions