100个练习学习Rust!构文・整数・变量

前一篇文章

【0】准备

【1】构文・整数・变量 ← 本次
全部文章列表

100 Exercise To Learn Rust》第2回,也就是实际演习的第1回!从这次开始,我们会适度减少前置说明,直接进入问题的解决!

本次的相关页面

[01_intro/01_syntax] 基本文法

问题如下:

rust 复制代码
// TODO: fix the function signature below to make the tests pass.
//  Make sure to read the compiler error message---the Rust compiler is your pair programming
//  partner in this course and it'll often guide you in the right direction!
//
// The input parameters should have the same type of the return type.
fn compute(a, b) -> u32 {
    // Don't touch the function body.
    a + b * 2
}

#[cfg(test)]
mod tests {
    use crate::compute;

    #[test]
    fn case() {
        assert_eq!(compute(1, 2), 5);
    }
}

"在Rust中,编译器是你最好的朋友,所以一定要认真听取编译器君的建议!" 大概是这个意思(非常意译)。实际上确实如此,所以这次我们首先来看一下编译错误。

rust 复制代码
$ wr
 

Running tests...

        ✅ (01) intro - (00) welcome (Skipped)
        ❌ (01) intro - (01) syntax

        Meditate on your approach and return. Mountains are merely mountains.



error: expected one of `:`, `@`, or `|`, found `,`
 --> exercises/01_intro/01_syntax/src/lib.rs:6:13
  |
6 | fn compute(a, b) -> u32 {
  |             ^ expected one of `:`, `@`, or `|`
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a `self` type, give it a parameter name
  |
6 | fn compute(self: a, b) -> u32 {
  |            +++++
help: if this is a parameter name, give it a type
  |
6 | fn compute(a: TypeName, b) -> u32 {
  |             ++++++++++
help: if this is a type, explicitly ignore the parameter name
  |
6 | fn compute(_: a, b) -> u32 {
  |            ++

error: expected one of `:`, `@`, or `|`, found `)`
 --> exercises/01_intro/01_syntax/src/lib.rs:6:16
  |
6 | fn compute(a, b) -> u32 {
  |                ^ expected one of `:`, `@`, or `|`
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
  |
6 | fn compute(a, b: TypeName) -> u32 {
  |                ++++++++++
help: if this is a type, explicitly ignore the parameter name
  |
6 | fn compute(a, _: b) -> u32 {
  |               ++

error: could not compile `syntax` (lib) due to 2 previous errors
error: could not compile `syntax` (lib test) due to 2 previous errors

一开始可能会被长长的错误信息压倒,但这些错误并不是随便给出的,而是大多提供了便于查找的错误信息。所以如果认真阅读,能学到很多东西。

虽然显示了两个错误,但它们几乎是相同的。

rust 复制代码
error: expected one of `:`, `@`, or `|`, found `,`
 --> exercises/01_intro/01_syntax/src/lib.rs:6:13
  |
6 | fn compute(a, b) -> u32 {
  |             ^ expected one of `:`, `@`, or `|`
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)

由于是语法问题,因此出现了与语法相关的错误。就像在做"理所当然体操"一样。

在错误信息下面有三条帮助提示,它们似乎根据我们的意图进行了分类。

  • 如果是 self 类型,请加上 self
  • self 是定义方法时的语法糖,这次不适用。
  • 如果是指参数(参数名),请写上类型。
  • 如果是指类型名,请加上参数名。 (虽然并没有这样直接写,而是提示使用 _ 来表示未使用的参数。)

这次的错误可能与第二点或第三点有关,即"应该同时明确写出参数名和类型,但只写了其中之一"。

在绑定变量时可以省略类型名(后面会详细说明,但必须静态确定),不过由于Rust是静态类型语言,所以在函数签名中特别需要明确指定类型。

解说

我们回到问题所在的部分。ab 是以小写字母开头的标识符。在Rust中,变量名通常采用蛇形命名法(snake_case),而类型名和特征名则采用帕斯卡命名法(PascalCase),因此这些标识符应该是变量名。

因此,我们需要为它们添加类型名。由于Rust不会进行隐式类型转换,为了最小化修改(也就是遵循"不要触碰函数体"的原则),将它们设为 u32 类型似乎是最合适的选择。

rust 复制代码
- fn compute(a, b) -> u32 {

+ fn compute(a: u32, b: u32) -> u32 {

    // Don't touch the function body.
    a + b * 2
}

成功通过了!让我们继续下一个问题。

(按顺序的话应该是 02_basic_calculator/00_intro,不过看起来只是做个标题调用,所以我们跳过它。后面的章节似乎也是类似的情况。)

[02_basic_calculator/01_integers] 整数型

问题如下:

rust 复制代码
fn compute(a: u32, b: u32) -> u32 {
    // TODO: change the line below to fix the compiler error and make the tests pass.
    a + b * 4u8
}

#[cfg(test)]
mod tests {
    use crate::compute;

    #[test]
    fn case() {
        assert_eq!(compute(1, 2), 9);
    }
}

虽然与刚才的问题类似,但这次更强调了 没有隐式类型转换 这一点。

解说

4u8 是一个表示 u8 类型的数字 4 的字面量。由于不会进行隐式类型转换,如果一开始就将其设定为 u32 类型的 4,效果会更好。

rust 复制代码
fn compute(a: u32, b: u32) -> u32 {
    // TODO: change the line below to fix the compiler error and make the tests pass.

-    a + b * 4u8

+    a + b * 4u32

}

u32u8 中的 u 代表无符号整数(unsigned integer),i32i8 中的 i 代表整数(integer)。在Rust中,为了防止出现错误,通常在不需要表示负数时会使用无符号整数。

虽然有很多关于二进制补码和字节宽度的讨论,但最终我们会关心"最大值和最小值到底是多少?" 这些值可以通过 MINMAX 方法来确认。

此外,虽然在本节中尚未提及,但 usize 可能是你最常见到的整数类型。之所以如此,是因为这个整数类型非常特殊,它可以用于数组的索引。换句话说,usize 是一种接近地址值的类型。在32位操作系统中,它是32位宽;在64位操作系统中,它是64位宽,因此被赋予了"size"这个名称。

[02_basic_calculator/02_variables] 变量

问题如下:

rust 复制代码
pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
    // TODO: define a variable named `distance` with the right value to get tests to pass
    //  Do you need to annotate the type of `distance`? Why or why not?

    // Don't change the line below
    distance / time_elapsed
}

#[cfg(test)]
mod tests {
    use crate::speed;

    #[test]
    fn case1() {
        assert_eq!(speed(0, 10, 10), 1);
    }

    #[test]
    fn case2() {
        assert_eq!(speed(10, 30, 10), 2);
    }

    #[test]
    fn case3() {
        assert_eq!(speed(10, 31, 10), 2);
    }
}

提示内容是:distance 变量尚未定义,请定义它。

解说

通过用终点坐标减去起点坐标可以得到距离,因此可以将其绑定为 distance 变量。

rust 复制代码
pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
    // TODO: define a variable named `distance` with the right value to get tests to pass
    //  Do you need to annotate the type of `distance`? Why or why not?

+    let distance = end - start;


    // Don't change the line below
    distance / time_elapsed
}

从练习的意图来看,似乎是想引导你思考:"既然是强类型静态语言,为什么不需要为 distance 指定类型(比如写作 let distance: u32 = ...)呢?" 其实,Rust具有相当强大的类型推断机制,如果可以从相关的运算等推断出类型,就不会出现错误。

另外,VSCode 的 rust-analyzer插件还具有一个功能,即"当类型可以被推断时,会以灰色显示该类型"。

"Rust一开始有点难上手,但自从 rust-analyzer 能帮忙添加类型注释后,就变得容易多了," 这是我朋友说的。而实际上,这个功能确实使得Rust既易于编写又保持对类型的严格要求,同时也让 VSCode + rust-analyzer 成为了最好的Rust编辑器组合。

总结

最后一个问题很有特点,通过这三道题我们了解到:

  • 函数的签名必须由人手动指定。
  • 作为一种强类型静态语言,显然不允许隐式类型转换。
  • 由于类型推断机制,变量的类型注释在一定程度上可以省略。

这表明,Rust并不是要求在所有地方都必须进行类型注释,而是在需要的地方,比如函数签名,才需要人手动进行类型注释。正因为这一特点,笔者认为,Rust的类型系统相比其他语言更容易上手。

下一篇文章: 【2】if・panic・练习

相关推荐
GoppViper16 分钟前
golang学习笔记24——golang微服务中配置管理问题的深度剖析
笔记·后端·学习·微服务·golang·配置管理
景天科技苑16 分钟前
【Go】Go语言中延迟函数、函数数据的类型、匿名函数、闭包等高阶函数用法与应用实战
后端·golang·回调函数·defer·匿名函数·闭包·go函数数据类型
敲代码的奥豆18 分钟前
C++:日期类的实现
开发语言·c++
看山还是山,看水还是。31 分钟前
c#进度条实现方法
c语言·开发语言·笔记·c#
孑么34 分钟前
C# 委托与事件 观察者模式
开发语言·unity·c#·游戏引擎·游戏程序
ZachOn1y36 分钟前
Java 入门指南:JVM(Java虚拟机)垃圾回收机制 —— 垃圾收集器
java·jvm·后端·java-ee·团队开发·个人开发
敲代码不忘补水44 分钟前
Python Pickle 与 JSON 序列化详解:存储、反序列化与对比
开发语言·python·json
蜡笔小新星1 小时前
机器学习和深度学习的区别
开发语言·人工智能·经验分享·深度学习·学习·机器学习
齐 飞1 小时前
使用jackson将xml和对象、List相互转换
xml·java·spring boot·后端·list
liwulin05061 小时前
java-在ANTLR中BaseListner的方法和词法规则的关系0.5.0
java·开发语言