writing rust module

Let's say you have a file called "mymodule.rs" in the folder and you want to import it in main.rs 

Approach #1 

mymodule.rs 

pub mod hello {
   
    pub fn hello() -> String
    {
        return String::from("hello world")
    }

    pub fn hello2() {
        println!("hello world")
    }
}

main.rs - it start off with mod mymodule (which matches the filename of your module). noticed I added another layer by having hello2() function under mod hello -- this gives you mymodule::hello::hello2()

mod mymodule;

fn main() {
    // initialize tracing
    // tracing_subscriber::fmt::init();

    mymodule::hello::hello2();
}


Approach #2

This could be another solution 


#[path = "mymodule.rs"] mod thing;
fn main() {
  thing::hello::hello2();
}

 There's a couple of other solution as well

https://stackoverflow.com/questions/26388861/how-can-i-include-a-module-from-another-file-from-the-same-project


Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm