Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

模块 mod

mod 组织代码结构;

内部模块 外部调用模块内函数—> pub 修饰符

mod mod_test{
    pub fn hello(){
        println!("hi");
    }
    pub mod mod_inner{
        pub fn hello1(){
            println!("hi inner mod");
        }
    }
}

fn main(){
    //这里要调用模块 mod_test()
    crate::mod_test::hello();
}
  • main函数要调用模块mod_test中的函数hello() ,函数必须要有权限修饰符pub
  • 模块之间调用,被调用模块同一个包下可以随便调用,否则也要pub修饰符修饰。

crate::mod_test::hello();

注解

写了代码却不用,会被警告⚠️,加上#[allow(unused)]注解 警告消除。