Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Modules and enums

  • Internally in the helper module we can use the enum.
  • If we return it in a public function (leaking it) we get a compiler warning.
  • If we try to use the public function in the outside world, we get a compile error.
  • We need to declare the enum to be pub. Then all its variants also become public.
fn main() {
    helper::show_animal();

    let animal = helper::get_animal();
    println!("{:?}", animal);
}

mod helper {

    #[allow(dead_code)]
    #[derive(Debug)]
    pub enum Animal {
        Cat,
        Dog,
    }

    pub fn show_animal() {
        let animal = Animal::Cat;
        println!("{:?}", animal);
    }

    pub fn get_animal() -> Animal {
        Animal::Dog
    }
}