Let us create a project with a binary crate:
cargo new --bin foo 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:
bat src/math/mod.rspub fn add(a: i16, b: i16) -> i16 {
a + b
}
The main code file is written to use the math module:
bat src/main.rsmod math;
fn main () {
println!("{}", math::add(2, 6));
}
The program still compiles:
cargo build Compiling foo v0.1.0 (/tmp/tmp.qF3xiE5d1v/foo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
And we can run it:
cargo run 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.