4

Main function is as follow:

mod stats;

fn main() {
    let raw_data = [10, 10, 20, 1, 2, 3, 5];
    let mut v: Vec<u32> = Vec::new();
    let mean = 0;
    let median = 0;
    let mode = 0;
    for i in raw_data.iter() {
        v.push(*i);
    }
    let stat = stats::stats::Stats::new(v);
}

And module stats as follow:

pub mod stats {
    pub struct Stats {
        data: Vec<u32>,
    }
    impl Stats {
        pub fn new(data: Vec<u32>) -> Stats {
            Stats { data }
        }
        pub fn find_mean(&self) -> f64 {
            let mut sum = 0;
            for i in &self.data {
                sum += i;
            }
            return (sum / self.data.iter().count() as u32) as f64;
        }
        pub fn find_mode(&self) -> u32 {}
        pub fn find_median(&self) -> f64 {}
    }
}

Why do I have to use stats::stats to reference struct Stats.

Project Structure

hellow
  • 12,430
  • 7
  • 56
  • 79

1 Answers1

6

Inside your stats.rs file you create another module stats which means, that you have to use stats::stats, because every file creates its own module.

To solve your issue, just remove pub mod stats in your stats.rs file.

For further information see:

ljedrz
  • 20,316
  • 4
  • 69
  • 97
hellow
  • 12,430
  • 7
  • 56
  • 79