首页 > 其他分享 >[Rust] module with public and private methods

[Rust] module with public and private methods

时间:2024-02-27 15:02:49浏览次数:20  
标签:sausage const methods snacks use pub private module fn

Methods:

mod sausage_factory {
    // private method
    fn get_secret_recipe() -> String {
        String::from("Ginger")
    }

    // public method
    pub fn make_sausage() {
        get_secret_recipe();
        println!("sausage!");
    }
}

fn main() {
    sausage_factory::make_sausage();
}

 

Interfaces:

mod delicious_snacks {
    // export those as interfaces
    pub use self::fruits::PEAR as fruit;
    pub use self::veggies::CUCUMBER as veggie;

    mod fruits {
        pub const PEAR: &'static str = "Pear";
        pub const APPLE: &'static str = "Apple";
    }

    mod veggies {
        pub const CUCUMBER: &'static str = "Cucumber";
        pub const CARROT: &'static str = "Carrot";
    }
}

fn main() {
    println!(
        "favorite snacks: {} and {}",
        delicious_snacks::fruit,
        delicious_snacks::veggie
    );
}

 

You can use the 'use' keyword to bring module paths from moudles from anywhere and especially from the Rust standard library into your scope.

Bring SystemTime and UNIX_EPOCH from the std::time module.

use std::time::{UNIX_EPOCH, SystemTime};

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
    }
}

 

标签:sausage,const,methods,snacks,use,pub,private,module,fn
From: https://www.cnblogs.com/Answer1215/p/18036875

相关文章

  • 查询pytest --version报错 AttributeError: module ‘collections‘ has no attribute
     卸载pytest及关联的插件先查询一下pytest及对应关联的插件 pipuninstallcoloramaexceptiongroupiniconfigpackagingpluggytomliallure-pytestpytest-allure-adaptorpytest回车,每次都回复Y,同意卸载 再重新安装pytestpipinstallpytest-ihttp://pypi.douba......
  • HarmonyOS—添加/删除Module
    Module是应用/服务的基本功能单元,包含了源代码、资源文件、第三方库及应用/服务配置文件,每一个Module都可以独立进行编译和运行。一个HarmonyOS应用/服务通常会包含一个或多个Module,因此,可以在工程中创建多个Module,每个Module分为Ability和Library两种类型。在工程中添加Module......
  • uniapp引入uview-基于最新HBuilder X 3.3-基于uni_modules
    uniapp引入uview-基于最新HBuilderX3.3-基于uni_modules:https://blog.csdn.net/billycoder/article/details/122795900?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170850542516800225563950%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%25......
  • nginx启动报错:ngx_http_image_filter_module.so" version 1016001 instead of 1022001
    问题现象,启动nginx,提示版本不对[root@k8s-test-node2modules]#/data/nginx/sbin/nginxnginx:[emerg]module"/usr/lib64/nginx/modules/ngx_http_image_filter_module.so"version1016001insteadof1022001in/usr/share/nginx/modules/mod-http-image-filter.conf:1......
  • 在script标签写export为什么会抛错|type module import ES5 ES6 预处理 指令序言 JavaS
    今天我们进入到语法部分的学习。在讲解具体的语法结构之前,这一堂课我首先要给你介绍一下JavaScript语法的一些基本规则。脚本和模块首先,JavaScript有两种源文件,一种叫做脚本,一种叫做模块。这个区分是在ES6引入了模块机制开始的,在ES5和之前的版本中,就只有一种源文件类型(就......
  • MAC DOCKER Zookeeper 启动报错 mkdir /host_mnt/private/var/db/timezone/tz: operat
    在Mac电脑上启动Zookeeper和kafka的docker容器时报错一开始我的语句是这样的dockerpullwurstmeister/zookeeperdockerrun-d--restart=always--log-driverjson-file--log-optmax-size=100m--log-optmax-file=2--namezookeeper-p2181:2181-v/etc/localtime:/et......
  • both methods have same erasure, yet neither overrides the other
    泛型,作为JDK5时代引入的”语法糖“,在编译的时候是会被抹除的,换言之,specialSort(List<Dog>)和specialSort(List<Apple>)在编译时都会变成specialSort(List),因此不符合重载的原则(变量名相同、参数类型或数量不同)。参考:https://blog.csdn.net/m0_37676618/article/details/106714182......
  • Angular 17+ 高级教程 – NgModule
    前言NgModule在Angularv14以前是一门必修课。然而,自Angularv14推出StandaloneComponent以后,它的地位变得越来越边缘化。本教程从开篇到本篇,所有例子使用的都是 StandaloneComponent,一点NgModule的影子也没有......
  • 谷歌新版本跨域错误深度剖析与解决:request client is not a secure context and the
    原文地址:https://blog.csdn.net/Flywithdawn/article/details/128253604 快速解决: ======================================================最近在测试http服务时,谷歌浏览器报了以下错误“Therequestclientisnotasecurecontextandtheresourceisinmore-privat......
  • CommonJS、AMD、CMD、ES Module
    依赖前置和依赖就近RequireJS采用依赖前置,举个例子就是炒菜前一次性把所有食材洗好,切好,根据内部逻辑把有些酱料拌好,最后开始炒菜,前面任何一个步骤出现问题都能较早发现错误;SeaJS的依赖就近就是要炒青椒了去切青椒要炒肉了去切肉cmd:例如seajsamd:例如requirejscommonjs模块规范nod......