[易学易懂系列|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

最新文章

  1. handlebars.js 用 <br>替换掉 内容的换行符
  2. 如何使用openssl生成RSA公钥和私钥对
  3. GHOST(幽灵)重大漏洞
  4. Linux运维教程
  5. IOS开发UI基础UITableView的属性
  6. [Hibernate] - many to one
  7. cocos2d-x 3.0 alpha1 生成Qt qch帮助文档
  8. redis学习-day1
  9. Node.js : 我只需要一个店小二
  10. Hadoop-2.2.0中文文档—— MapReduce下一代- 可插入的 Shuffle 和 Sort
  11. DZY Loves Partition
  12. FPGA在其他领域的应用(二)
  13. python笔记:#002#第一个python程序
  14. 使用tensorflow搭建自己的验证码识别系统
  15. Android : 关于HTTPS、TLS/SSL认证以及客户端证书导入方法
  16. topcoder srm 702 div1 -3
  17. php能力自测
  18. linux下编写简单的守护进程
  19. 【树形dp】The more, The Better
  20. java 定时器的三种方式

热门文章

  1. sshpass密码
  2. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
  3. AI测试——旅程的终点
  4. Spring中用到了哪些设计模式?
  5. Akka简介与Actor模型(一)
  6. mongodb 后台启动命令记录
  7. 【Linux 网络编程】REUSADDR
  8. coredump产生的几种可能情况
  9. linux 使用tmux
  10. Codeforces 1201E2. Knightmare (hard)