Rust能力养成系列之(19) :类型系统和及其重要性

前言

 

前面三章都是Rust的基础和 前提性内容,到了第4章,终于开始涉及Rust语言的核心内容了:类型(Types),泛型(Generics),特性(Traits, 讲真,翻译成特性还是有点问题,时下姑且用之)。

Rust的类型系统是该语言的显著特征之一。在本章中,我们将详细讨论该语言的一些重要方面,如特性、泛型,以及如何使用它们来编写颇具表达力的代码。同时,还将探索一些有助于编写Rust语言风格代码的标准库特性。可以说,这一章非常有料。

 

我们将涵盖以下主题:

  • 类型系统和重要性
  • 泛型编程
  • 基于特性的增强类型
  • 标准库特性探索
  • 串烧特性与泛型的高表现力代码

 

类型系统和及其重要性

为什么编程语言中需要类型?这是一个很好的问题,可以作为理解程序语言类型系统的动机。作为程序开发者,我们知道,计算机程序在其底层就是所谓的0和1组成的二进制编码。事实上,最早的计算机不得不用机器代码手动编程,那真是一个难以想象的时代。最终,当时的程序员们意识到这是非常容易出错、乏味和耗时,而且人脑在二进制层面操纵和推断代码和效果非常不实用。后来,在20世纪50年代,编程界提出了所谓的机器代码助记法(machine code mnemonics),随之变成了我们今天所知道的汇编语言(assembly language)。再之后,现在意义上的程序设计语言出现了,它可以编译成汇编代码,并允许程序员编写人类可读的代码,而计算机也可以很容易的编译成机器代码。然而,由于人类使用的语言可能非常模糊,因此需要设置一套规则和约束,也就是进行日常语言的形式化,目的是来考察,传达和应用:在计算机程序语言中什么是可能的,什么是不可能的。这就是语义(semantics),有基于此,自然引出了类型和类型系统(types and type systems)的概念。

就定义而言,类型是可能值的命名集合(A type is a named set of possible values)。例如,u8只能为0 ~ 255的正整数。类型为我们提供了一种方法,可以弥合低级表征和人们为这些实体所创建的思想模型之间的鸿沟。除此之外,类型还为我们提供了一种方式来表达实体的意图、行为和约束,以此而定义了我们可以用类型做什么,不能用类型做什么。例如,并没有这样的定义:将字符串类型的值添加到数字类型的值中。从类型出发,语言设计人员构建了类型系统,它是一组规则,控制编程语言中不同类型如何相互交互,因而充当了程序推断的工具,并有助于确保我们的程序行为正确,并符合规范。类型系统的限定基于其可表达性,这仅仅意味着使用类型系统,只可以已有的逻辑规范下进行一定程度的语义表达,远远达不到日常语言的灵活度,这方面的提升还要仰仗于语用学的发展和突破。例如,Haskell是一种高级语言,它有一个非常有表现力的类型系统,而C是一种低级语言,提供给我们基于类型的抽象非常之少。Rust则试图在这两个极端之间做一个恰当的平衡。

Rust的类型系统很大程度上受到了函数式语言(functional language)的启发,比如Ocaml和Haskell中的ADTs(Abstract Data Type),影响了Rust中的枚举和结构,而Rust的特性(trait)则类似于Haskell中的类型类(typeclasses),再就是错误处理类型(Option和Result)也是非Rust独创。Rust类型系统的特征突出的是一个强类型系统,这意味着它在编译时执行会更多的类型检查,而不是等在运行时才进行大规模的类型检查。此外,类型系统是静态的(static),比如一个绑定到整数值的变量以后不能更改为指向字符串。可见,这些特性有助于生产健壮的程序代码,而且很少会在运行时影响到常量,但代价就是,在编写程序过程中,需要程序员进行一些相对缜密的计划和思考。一定程度上,可以说Rust要求开发者在设计程序时要考虑再三,这可能会影响一些程序员快速创建原型的节奏。然而,从维护软件系统的长期角度来看,这绝非是一件负面的事情。

除此之外,Rust语言的类型系统对于代码复用,也颇有其独到之处,而这是我们马上就要介绍的内容。

 

结语

 

类型系统,在实质上,作为一个有效的媒介,建立起人脑与电脑之间的高效通道,从而使人类的能力通过计算机得到了空前的延伸和增强,而逻辑学,语言学以及其他认知科学,在这其中扮演中基础和提升的角色,而这同时也是程序语言开展进化的一个重要切入点。

 

主要参考和建议读者进一步阅读的文献

https://doc.rust-lang.org/book

Rust编程之道,2019, 张汉东

The Complete Rust Programming Reference Guide,2019, Rahul Sharma,Vesa Kaihlavirta,Claus Matzinger

Hands-On Data Structures and Algorithms with Rust,2018,Claus Matzinger

Beginning Rust ,2018,Carlo Milanesi

Rust Cookbook,2017,Vigneshwer Dhinakaran

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
rust 数据结构与算法,英文版,epub格式 Chapter 1, Hello Rust!, gives a short recap of the Rust programming language and what changed in the 2018 edition. Chapter 2, Cargo and Crates, discusses Rust's cargo build tool. We will explore the configuration as well as the build process and modularization options. Chapter 3, Storing Efficiently, looks at how in Rust, knowing where values are stored is not only important for performance, but also important for understanding error messages and the language in general. In this chapter, we think about stack and heap memory. Chapter 4, Lists, Lists, and More Lists, covers the first data structures: lists. Using several examples, this chapter goes into variations of sequential data structures and their implementations. Chapter 5, Robust Trees, continues our journey through popular data structures: trees are next on the list. In several detailed examples, we explore the inner workings of these efficient designs and how they improve application performance considerably. Chapter 6, Exploring Maps and Sets, explores the most popular key-value stores: maps. In this chapter, techniques surrounding hash maps; hashing; and their close relative, the set; are described in detail. Chapter 1, Hello Rust!, gives a short recap of the Rust programming language and what changed in the 2018 edition. Chapter 2, Cargo and Crates, discusses Rust's cargo build tool. We will explore the configuration as well as the build process and modularization options. Chapter 3, Storing Efficiently, looks at how in Rust, knowing where values are stored is not only important for performance, but also important for understanding error messages and the language in general. In this chapter, we think about stack and heap memory. Chapter 4, Lists, Lists, and More Lists, covers the first data structures: lists. Using several examples, this chapter goes into variations of sequential data structures and their implementations. Chapter 5, Robust Trees, continues our journey through popular data structures: trees are next on the list. In several detailed examples, we explore the inner workings of these efficient designs and how they improve application performance considerably. Chapter 6, Exploring Maps and Sets, explores the most popular key-value stores: maps. In this chapter, techniques surrounding hash maps; hashing; and their close relative, the set; are described in detail.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值