[易学易懂系列|rustlang语言|零基础|快速入门|(18)|use关键词]
[易学易懂系列|rustlang语言|零基础|快速入门|(18)|use关键词]
实用知识
use关键词
我们今天来讲讲use关键词。
1.简单来说,use是给其他方法或资源定义一个别名,然后调用者,就可以直接用这个别名来调用,从而简化代码。
看下例子吧,我们先来看看没有用use的代码:
// -- Initial code without the `use` keyword --
mod phrases {
pub mod greetings {
pub fn hello() {
println!("Hello, world!");
}
}
}
fn main() {
phrases::greetings::hello(); // Using full path
}
如果没有用use,我们的代码是很繁琐的。
我们现在用use,来看看有什么效果:
// -- Usage of the `use` keyword --
// 01. Create an alias for module
use phrases::greetings;
fn main() {
greetings::hello();
}
这里,我们看到是对模块phrases和子模块greetings,创建一个别名:phrases::greetings。
当然,我们也可以对模块中的元素进行重新创建别名,看代码:
// 02. Create an alias for module elements
use phrases::greetings::hello;
fn main() {
hello();
}
当然,我们也可以把这个别名重新命名,看代码:
// 03. Customize names with the `as` keyword
use phrases::greetings::hello as greet;
fn main() {
greet();
}
2.导入元素到作用域
我们之前的测试例子,其实就用到这个功能。
请看代码:
fn hello() -> String {
"Hello, world!".to_string()
}
#[cfg(test)]
mod tests {
use super::hello; // Import the `hello()` function into the scope
#[test]
fn test_hello() {
assert_eq!("Hello, world!", hello()); // If not using the above `use` statement, we can run same via `super::hello()`
}
}
其中,super代表当前模块从父模块导入相关资源。那我们可以推断self就代表当前模块了。
是的,正确。
现在我们再来看看标准库的use用法 :
// -- 01. Importing elements --
use std::fs::File;
fn main() {
File::create("empty.txt").expect("Can not create the file!");
}
// -- 02. Importing module and elements --
std::fs::{self, File} // `use std::fs; use std::fs::File;`
fn main() {
fs::create_dir("some_dir").expect("Can not create the directry!");
File::create("some_dir/empty.txt").expect("Can not create the file!");
}
// -- 03. Importing multiple elements --
use std::fs::File;
use std::io::{BufReader, BufRead}; // `use std::io::BufReader; use std::io::BufRead;`
fn main() {
let file = File::open("src/hello.txt").expect("file not found");
let buf_reader = BufReader::new(file);
for line in buf_reader.lines() {
println!("{}", line.unwrap());
}
}
3.重新暴露
这里的重新暴露,是指通过一个固定模块,把子模块的相关资源暴露给外部。
看代码:
// ↳ main.rs
mod phrases;
fn main() {
phrases::hello(); // Not directly map
}
// ↳ phrases/mod.rs
pub mod greetings;
pub use self::greetings::hello; // Re-export `greetings::hello` to phrases
// ↳ phrases/greetings.rs
pub fn hello() {
println!("Hello, world!");
}
以上,希望对你有用。
如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
参考文章:https://learning-rust.github.io/docs/d6.use.html
[易学易懂系列|rustlang语言|零基础|快速入门|(18)|use关键词]的更多相关文章
- [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]
[易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]
[易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]
[易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]
[易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]
[易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]
[易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]
[易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]
[易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]
[易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]
[易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...
随机推荐
- java:sso(单点登录(single sign on),jsp文件动静态导入方式,session跨域)
1.jsp文件导入: 2.session跨域: 3.sso(单点登录(single sign on): sso Maven Webapp: LoginController.java: package ...
- Skywalking部署+NetCore客户端
一.介绍 应用官方的说法:Apache SkyWalking是观察性分析平台和应用性能管理系统.提供分布式追踪.服务网格遥测分析.度量聚合和可视化一体化解决方案 GitHub地址:https://gi ...
- PJzhang:shell基础入门的2个疗程-one
猫宁!!! 在centos7上操作这一切 第1节:什么是shell centos7默认使用shell的bash cat /etc/shells 第2节:linux的启动过程 BIOS(主板,引导介质) ...
- eclipse SVN插件的日常使用
安装(我的安装方法,怎么方便怎么来) 1.打开eclipse,选择Help->Eclipse MarketPlace,搜索subclipse,点击install,等待.安装成功后会询问重启,点击 ...
- C学习笔记-gcc
GNU CC(通常称为GCC)是GNU项目的编译器,它能够编译C.C++语言编写的程序 gcc的优点 使用gcc,程序员可以控制生成二进制执行文件中调试代码的数量和类型. 和其他编译器一样,gcc也可 ...
- 使用Python过程中遇到的一些坑及其解决方法(持续更新)
1.列表不能直接赋值 nums1 = nums2 x nums1[:] = nums2 正确 2.返回列表某一元素的值可以使用index函数 aList = [123, 'xyz', 'runoob' ...
- IIS 404设置
想给自己做的的网站自定义一个404页面,开始 双击红框提示的错误页图标 双击上图红框提示的所示404行 修改上图红框提示的内容如下:我是直接在根目录放了一个自己做的404.html,实际情况要填写你自 ...
- Git和Github的使用
Git和Github的基本操作 一.了解Git和Github 1.什么是GIT? Git是一个免费.开源的版本控制软件 2.什么是版本控制系统? 版本控制是一种记录一个或若干个文件内容变化,以便将来查 ...
- 从入门到自闭之Python随机模块
导入:import random 随机小数:random.random():大于0小于1之间的小数 指定数字之间的小数,不包含指定的最大值:random.uniform() 随机整数:random.r ...
- Java常见数据结构
HashMap深入浅出 HashMap数据结构 HashMap的本质就是一个数组加链表,数组默认长度是16,存储的元素达到总长度的75%就会扩容一倍.map.put(key,val),实际上就是根据h ...