How to call functions inside a module tree

Hi,

Could anyone explain what am I doing wrong in the following example:

In main.rs

 mod inc::api;
 use inc::api::Get;
 fn main() {
    println!("Hello, world!");
    let a = api::Api::new("Hi there\n".to_string());

    let e = a.get();
    println!("{}", e);
}

in my scr I have:

src
  |
  +----- main.rs
  |
  +-----inc 
          |
          +----api
                 |
                 +----mod.rs

where mod.rs looks like :

#[derive(Debug)]
pub struct Api{
    Line: String
}


pub trait Get {
    fn get (&self)-> &str;
    fn join (a: String, b: String)-> String;
} 



impl Api {
    pub fn new(s: String)->Self{
        Api {Line :  Api::join(s, " This is my API !\n".to_string())}
    }    
}

impl Get for Api {
    fn get(&self)-> &str{
        &self.Line
    }
    fn join(a: String, b: String)->String{
        format!("{}{}.", a, b)
    }
}

when I try to compile I get:

error: expected one of `;` or `{`, found `::`                                                                                                                            
 --> src/main.rs:2:8                                                                                                                                                     
  |                                                                                                                                                                      
2 | mod inc::api;                                                                                                                                                        
  |        ^^ expected one of `;` or `{` here 

Why is that ??

mod works the same as fn or struct, i.e. it defines a new item in the place where you put this declaration.

You can't do fn foo::bar::baz() {}, but you have to put fn baz() in the right module. Same for mod placement.

1 Like

aha... thnx :slight_smile: