zoukankan      html  css  js  c++  java
  • 【转】Modules in Rust

    原文:https://www.educative.io/edpresso/modules-in-rust

    ---------------------------

    In Rust, modules are containers that enclose zero or more items (i.e., functions, structs, traits, impl blocks, and nested modules). These containers help to hierarchically organize code into logical units and then govern those units’ scope.

    svg viewer

    Code

    The mod keyword is used to define a module item. By default, any item inside a module is private, so it cannot be accessed from outside that module. The pub keyword can be used to change this and give that item public visibility. The code below shows how we can use these keywords:

    mod external_mod {
        
        pub fn public_func() {
            println!("Public function called\n");
        }
    
        fn private_func(){
            println!("Private function called\n");
        }
        
        pub fn private_func_accessor() {
            println!("Accessing private function with a public function");
            private_func();
        }
    
        mod internal_mod {
            pub fn public_internal_func() {
                println!("Public function inside internal mod called\n");
            } 
        }
    
        pub fn internal_mod_accessor() {
            println!("Accessing internal mod with a public function");
            internal_mod::public_internal_func();
        }
    }
    
    fn main() {
        external_mod::public_func();
    
        external_mod::private_func_accessor();
    
        external_mod::internal_mod_accessor();
    }
    

      

  • 相关阅读:
    ctrl+shift+k取消
    ERROR 1872
    swap
    mysql主从跳过错误
    undo
    gtid
    falcon监控指标
    连接数
    datetime与timestamp相互转换
    截取文件内容
  • 原文地址:https://www.cnblogs.com/oxspirt/p/15600936.html
Copyright © 2011-2022 走看看