To create a module, we first need to tell the compiler that the code is held in a module. In this example, I will use the Trigonometry module:
mod Trigonometry // top level module { mod Natural // sub module { } mod Arc // sub module { } }
When we compile this using cargo build (not cargo run; there is no main function within the library) and examine the tree, we'll see the library (highlighted):
We can't do very much with it currently as all it contains are placeholders that do very little. Before this goes any further, have a look at the lib.rs source file. With nothing in except for the module names, it hits 62 lines. Let's think of a very simple example for the Conversion module, fahrenheit_to_celcius.
The formula to do this is (F - 32) * 5/9. Our function will therefore be the following:
pub fn fahrenheit_to_celcius(a: f32) -> f32 { (a - 32f32) * 5f32 / 9f32 }
That was just four lines of code. We also need ones to go from C to F, K to C, C to K, F to K, and K to F (K is Kelvin, which denotes absolute temperature, that is, 0K = -273.15oC , also known as absolute zero). Including these will take it to a total of around 24 lines of code. This is a simple module. The ones for regression analysis run to around 100.
Our source file is going to be huge. As we want to keep our modules manageable, we are going to need to break the lib.rs file down a bit.