K
K
keddad2019-05-28 15:03:02
Rust
keddad, 2019-05-28 15:03:02

What is mod in Rust?

How (and what are) modules in Rust? I found this example , but I don't understand why it's necessary. Is it something like a namespace for functions inside?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2019-05-28
@keddad

mod is a child module/namespace
declaration. There are 2 ways to declare it - via a block and via a separate file:

// через блок
mod some_module {
  // тут изолированное пространство имен
  pub fn hello() {
    println!("Hello world");
  }
}
fn main() {
  some_module::hello(); // Hello world
}

// подключаем модуль из файла ./some_other_module/mod.rs или ./some_other_module.rs
mod some_other_module;

also, through the pub operator, you can re-export the entire module, thereby making the namespace structure of your crate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question