How to call functions inside a module tree



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


 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:


where looks like :

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{
    fn join(a: String, b: String)->String{
        format!("{}{}.", a, b)

when I try to compile I get:

error: expected one of `;` or `{`, found `::`                                                                                                                            
 --> src/                                                                                                                                                     
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.


aha… thnx :slight_smile: