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

原文地址:https://www.cnblogs.com/gyc567/p/12009907.html

时间: 2024-08-29 16:01:37

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

[易学易懂系列|rustlang语言|零基础|快速入门|(14)]

有意思的基础知识 Impls & Traits实现与特征 我之前说到的struct结构体,其实就类似于面向对象语言中的类class. 但这个struct,并没有定义方法或函数. 那要怎么办呢? Rust用关键词impls(实现)来定义struct和enum的方法或函数. 而trait(特征),类似于面向对象语言中的接口interface. 特征,是用来定义要实现的方法,一个类型可以有多个特征.特征可以有默认实现函数,这个默认函数可以在运行时重写. 我们来看看代码: 1.没有trait特征的imp

[易学易懂系列|rustlang语言|零基础|快速入门|(12)]

有意思的基础知识 Enums 今天我们来讲讲枚举. 在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数.这两种类型经常(但不总是)重叠. 是一个被命名的整型常数的集合,枚举在日常生活中很常见,例如表示星期的SUNDAY.MONDAY.TUESDAY.WEDNESDAY.THURSDAY.FRIDAY.SATURDAY就是一个枚举. 如下定义: enum Day { Sunday, Monday, Tuesday, Wednesday, Thu

[易学易懂系列|rustlang语言|零基础|快速入门|(13)]

有意思的基础知识 Generics泛型 我们今天来看看泛型. 什么是泛型? 我们来看看这样的情景: 我们要写一个函数,这个函数可以处理不同类型的值,但这个值的类型,在运行时,才由调用者确定. 我们不可能在函数方法中,一开始就写死. 那要什么办? 用泛型. 比如:用x : T替换x : u8 我们来看看例子: 泛型函数: fn takes_anything<T>(x: T) { // x has type T, T is a generic type } fn takes_two_of_the_

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

实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值.这个值,指向堆heap的地址. 什么是智能指针? 在Rust,简单来说,相对普通指针,智能指针,除了保存内存地址外,还有额外的其他属性或元数据. 在Rust中,因为有所有权和借用的概念,所以引用和智能指针,又有一点不一样. 简单来说,智能指针,拥有数据所有权,而引用没有. 智能指针分以下几种: 1.Box,用于在堆里分配内存. 2.Rc,引用计数类型,用于多线程中的多个所有

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

项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的角度上来说,区块链,就是区块组成的链. 以下就是BTC区块链典型的结构: 那最小单元就是区块:block. 这个block包含两部分:区块头,区块体. 我们先忽略Merkle树,先简化所有数据结构,只保留最基本的数据结构. 那区块头,就包含:时间截:前一个区块地址 区块体,就包含交易数据,我们用一个vector来存储. 代码如下 : ///交易结构体 #[derive(Clone, Hash,

[易学易懂系列|rustlang语言|零基础|快速入门|(29)|实战6:BDD工具cucumber_rust]

项目实战 实战6:BDD工具cucumber_rust 今天我们来学习下BDD行为驱动测试工具cucumber_rust. 关于BDD,可以简单看看这些介绍: https://www.cnblogs.com/superhin/p/11454716.html#bdd%E4%BB%8B%E7%BB%8D https://www.jianshu.com/p/0389360ac58f https://www.infoq.cn/article/2011/02/BDD-ATDD 简单来说,BDD是一种更好,

[易学易懂系列|rustlang语言|零基础|快速入门|(17)|装箱crates]

实用知识 装箱crates 我们今天来讲讲装箱技术crates. 什么是crates? 英语翻译是: 英 [kre?t] 美 [kre?t] n. 板条箱:篓 vt. 将某物装入大木箱或板条箱中 [ 过去式 crated 过去分词 crated 现在分词 crating 复数 crates 第三人称单数 crates ] 其实,它也就是一种模块化封装技术. 我们还是来看看代码,我们先用命令:cargo new greetings 生成一个新的工程,工程目录如下 : // # It generat

小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_1、SpringBoot2.x课程介绍和高手系列知识点

1 ======================1.零基础快速入门SpringBoot2.0 5节课 =========================== 1.SpringBoot2.x课程全套介绍和高手系列知识点     简介:介绍SpringBoot2.x课程大纲章节         java基础,jdk环境,maven基础 2.SpringBoot2.x依赖环境和版本新特性说明 简介:讲解新版本依赖环境和springboot2新特性概述 1.依赖版本jdk8以上, Springboot2

零基础快速入门web学习路线(含视频教程)

下面小编专门为广大web学习爱好者汇总了一条完整的自学线路:零基础快速入门web学习路线(含视频教程)(绝对纯干货)适合初学者的最新WEB前端学习路线汇总! 在当下来说web前端开发工程师可谓是高福利.高薪水的职业了.所以现在学习web前端开发的技术人员也是日益增多了,但是在学习web前端开发中盲目的去学习而没有一个完整的思路和学习路线也是不行的. 成为一个合格的web前端开发工程师的具备什么条件? 熟练的掌握HTML.CSS.JS.JQ等最基本的技术. 现在,只掌握这些已经远远不够了.无论是开