Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Let us create a project with a binary crate:

    Creating binary (application) `foo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

A module can also be written as a folder, in which case a file mod.rs must be created inside the folder. For instance if we want to define our math module as a folder, we need to create a file src/math/mod.rs.

Here is the content of our file:

pub fn add(a: i16, b: i16) -> i16 {
  a + b
}

The main code file is written to use the math module:

mod math;

fn main () {
  println!("{}", math::add(2, 6));
}

The program still compiles:

   Compiling foo v0.1.0 (/tmp/tmp.qF3xiE5d1v/foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s

And we can run it:

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/foo`
8

The advantage of using a folder module is that we can define file sub-modules.