[易学易懂系列|rustlang语言|零基础|快速入门|(18)|use关键词]

释放双眼,带上耳机,听听看~!

[易学易懂系列|rustlang语言|零基础|快速入门|(18)|use关键词]

实用知识

use关键词

我们今天来讲讲use关键词。

1.简单来说,use是给其他方法或资源定义一个别名,然后调用者,就可以直接用这个别名来调用,从而简化代码。

看下例子吧,我们先来看看没有用use的代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
1// -- Initial code without the `use` keyword --
2mod phrases {
3  pub mod greetings {
4    pub fn hello() {
5      println!("Hello, world!");
6    }
7  }
8}
9
10fn main() {
11  phrases::greetings::hello(); // Using full path
12}
13

如果没有用use,我们的代码是很繁琐的。

我们现在用use,来看看有什么效果:


1
2
3
4
5
6
7
1// -- Usage of the `use` keyword --
2// 01. Create an alias for module
3use phrases::greetings;
4fn main() {
5  greetings::hello();
6}
7

这里,我们看到是对模块phrases和子模块greetings,创建一个别名:phrases::greetings。

当然,我们也可以对模块中的元素进行重新创建别名,看代码:


1
2
3
4
5
6
1// 02. Create an alias for module elements
2use phrases::greetings::hello;
3fn main() {
4  hello();
5}
6

当然,我们也可以把这个别名重新命名,看代码:


1
2
3
4
5
6
1// 03. Customize names with the `as` keyword
2use phrases::greetings::hello as greet;
3fn main() {
4  greet();
5}
6

2.导入元素到作用域

我们之前的测试例子,其实就用到这个功能。

请看代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1fn hello() -> String {
2  "Hello, world!".to_string()
3}
4
5#[cfg(test)]
6mod tests {
7  use super::hello; // Import the `hello()` function into the scope
8
9  #[test]
10  fn test_hello() {
11    assert_eq!("Hello, world!", hello()); // If not using the above `use` statement, we can run same via `super::hello()`
12  }
13}
14

其中,super代表当前模块从父模块导入相关资源。那我们可以推断self就代表当前模块了。

是的,正确。

现在我们再来看看标准库的use用法 :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1// -- 01. Importing elements --
2use std::fs::File;
3
4fn main() {
5    File::create("empty.txt").expect("Can not create the file!");
6}
7
8
9// -- 02. Importing module and elements --
10std::fs::{self, File} // `use std::fs; use std::fs::File;`
11
12fn main() {
13    fs::create_dir("some_dir").expect("Can not create the directry!");
14    File::create("some_dir/empty.txt").expect("Can not create the file!");
15}
16
17
18// -- 03. Importing multiple elements --
19use std::fs::File;
20use std::io::{BufReader, BufRead}; // `use std::io::BufReader; use std::io::BufRead;`
21
22fn main() {
23    let file = File::open("src/hello.txt").expect("file not found");
24    let buf_reader = BufReader::new(file);
25
26    for line in buf_reader.lines() {
27        println!("{}", line.unwrap());
28    }
29}
30

3.重新暴露

这里的重新暴露,是指通过一个固定模块,把子模块的相关资源暴露给外部。

看代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1// ↳ main.rs
2mod phrases;
3
4fn main() {
5    phrases::hello(); // Not directly map
6}
7
8// ↳ phrases/mod.rs
9pub mod greetings;
10
11pub use self::greetings::hello; // Re-export `greetings::hello` to phrases
12
13// ↳ phrases/greetings.rs
14pub fn hello() {
15  println!("Hello, world!");
16}
17

以上,希望对你有用。


1
2
1如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
2

参考文章:https://learning-rust.github.io/docs/d6.use.html

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++中引用和指针的区别

2022-1-11 12:36:11

安全漏洞

检测不出的 PLC rootkit 终于现世

2016-12-26 8:55:30

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索