[易学易懂系列|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关键词]的更多相关文章

  1. [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]

    [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...

  2. [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]

    [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...

  3. [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...

  4. [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...

  5. [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...

  6. [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]

    [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...

  7. [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]

    [易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...

  8. [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]

    [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...

  9. [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]

    [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]

    [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...

随机推荐

  1. localStorage基本了解及使用

    以下内容来自: https://www.cnblogs.com/st-leslie/p/5617130.html  感谢大佬的分享 一.什么是localStorage.sessionStorage 在 ...

  2. EncryptHelper加密对象-工具类

    using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.W ...

  3. java:面向对象(接口(续),Compareble重写,Comparator接口:比较器的重写,内部类,垃圾回收机制)

    接口: *接口定义:使用interface关键字 * [修饰符] interface 接口名 [extends 父接口1,父接口2...]{ * //常量的声明 * //方法的声明 * } *接口成员 ...

  4. windows下sqlplus怎么连接远程oracle

    语法:sqlplus usr/pwd@//host:port/sid [oracle@mzl ~]$ sqlplus system/51411482@//192.168.21.11:1521/orcl ...

  5. core python

    一:正则表达式 闭包操作符 | 等同于 or   exp:a|b|c           . 匹配任意一个字符 (若匹配本字符,需转义使用 \.   不能匹配换行符\n及空字符串)    (^:匹配首 ...

  6. CISCO路由器WAN口动态ISP配置

        Building configuration... version 15.0 service timestamps debug datetime msec service timestamps ...

  7. MSF魔鬼训练营-3.5.3 MSF中常用的关于数据库的命令

    渗透测试数据库用来保存渗透测试过程中获取的各种数据,很多时候你重启了kali发现连接不上数据库了,其实就是因为这个服务没开 MSF所使用的数据库时postgresql root@kali:/# ser ...

  8. Python全栈开发之3、深浅拷贝、变量和函数、递归、函数式编程、内置函数

    一.深浅拷贝 1.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy # 定义变量 数字.字符串 # n1 = 123 n1 ...

  9. windows下命令行利器---Cmder(安装,中文乱码,配置右键菜单)

    很多人都是在win下开发的,这样就会出现,经常需要命令行操作,而win cmd命令和linux命令有很大差异,导致大家很难受,今天给大家介绍一个win下命令行的利器-Cmder 一.先看一下它的容颜 ...

  10. oracle 插入数据之坑--------oracle字符类型varchar2一个中文占多少字节

    如果你误认为是两个字节,那就大错特错了 Oracle 一个中文汉字 占用几个字节,要根据Oracle中字符集编码决定 查看oracle server端字符集 select userenv('langu ...