Exercise – Modules

Follow the video!

https://academy.moralis.io/lessons/exercise-modules

Let us know if you have any questions.

modulator/src/main.rs

use modulator::dependency::mod_one::one as dependency;

fn main() {
    let secret1 = modulator::secret::f1();

    let secret2 = modulator::f3();
    println!("data: {} {}", secret1, secret2);
    println!("One from dependency: {}", dependency());
}

modulator/src/lib.rs

pub mod dependency;

pub mod secret {
    fn f2() -> u32 {
        return 42;
    }

    pub fn f1() -> u32 {
        return f2();
    }
}

pub fn f3() -> u32 {
    return secret::f1();
}

modulator/src/dependency.rs

pub mod mod_one {
    pub fn one() -> u32 {
        return 1;
    }
}

modulator/src/bin/binary_crate.rs

use std::cmp as cmp;
// use modulator::dependency::mod_one::one as dependency;
use modulator::secret as secret;

fn main() {
    println!("Binary crate: {}", modulator::secret::f1());
    println!("Dependency from binary crate: {}", secret::f1());
    println!(
        "One from dependency: {}", modulator::dependency::mod_one::one());
    
    println!("{:?} {:?} {:?}", cmp::Ordering::Greater, cmp::Ordering::Less, cmp::Ordering::Equal);
}
1 Like

Here is my code.

modulator/src/main.rs
use modulator::dependency::mod_one::one as d;

fn main() {
    let secret1 = modulator::secret::f1();
    let secret2 = modulator::f3();
    println!("data: {} {}", secret1, secret2);
    println!("one from dependency: {}", d());
}
modulator/src/lib.rs
pub mod dependency;

pub mod secret {
    fn f2() -> u32 {
        return 42;
    }
    pub fn f1() -> u32 {
        return f2();
    }
}

pub fn f3() -> u32 {
    return secret::f1();
}
modulator/src/dependency.rs
pub mod mod_one {
    pub fn one() -> u32 {
        return 1;
    }
}
modulator/src/bin/binary_crate.rs
// cmp::Ordering::Greater
use modulator::secret as secret;
use std::cmp as cmp;

fn main() {
    println!(
        "{:?} {:?} {:?}",
        cmp::Ordering::Less,
        cmp::Ordering::Greater,
        cmp::Ordering::Equal
    );
    println!(
        "one from dependency: {}", 
        modulator::dependency::mod_one::one()
    );
    println!("Binary crate: {}", modulator::secret::f1());
    println!("Dependency from binary crate: {}", secret::f1());
}

Available binaries:

cargo run --bin modulator
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\modulator.exe`
data: 42 42
one from dependency: 1
cargo run --bin binary_crate
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\binary_crate.exe`
Less Greater Equal
one from dependency: 1
Binary crate: 42
Dependency from binary crate: 42
1 Like