Giter Site home page Giter Site logo

zhanghandong / tao-of-rust-codes Goto Github PK

View Code? Open in Web Editor NEW
1.2K 41.0 166.0 104.95 MB

《Rust编程之道》随书源码

Home Page: https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/

License: MIT License

Rust 98.24% C 0.39% Makefile 0.07% C++ 0.12% JavaScript 0.58% Python 0.33% Ruby 0.22% HTML 0.05%
rust tao-of-rust rust-study

tao-of-rust-codes's Introduction

MIT licensed crates.io Released API docs

English Version

The Tao of Rust: Table of Contents

豆瓣书评

希望大家看完之后,可以给个评价。

《Rust编程之道》豆瓣书评

购买地址:各大电商平台


img4

《Rust编程之道》随书源码

本书现在已出版。

跟进Rust 2018最新特性!!

跟进Rust 2018最新特性!!

跟进Rust 2018最新特性!!

说明:本仓库代码暂时不接受任何PR,因为很多都是随书源码案例,其中代码有专门的演示作用。如有异议,可以提交issues讨论。

注意:本书代码的平台是Linux或macOS

书籍勘误说明

  1. 提交issues
  2. 表明具体的「页码」、「行数」和「出错」信息
  3. 积极勘误者将获得Rust勋章一枚

Rust版本依赖:

  • Rust stable 1.31.0 +
  • Rust Nightly 1.31.0 +

输出文档时指定target目录:

$ cargo doc --target-dir ./

使用说明:

$ git clone https://github.com/ZhangHanDong/tao-of-rust-codes.git
$ cd tao-of-rust-codes
$ cargo doc
$ open target/doc/tao_of_rust/index.html

在线阅读:

or:

图示

img1 img2 img3

点击Run,跳转到play.rust-lang.org img4

本书完整目录

TOC

tao-of-rust-codes's People

Contributors

dev-chee avatar matrixbirds avatar zhanghandong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tao-of-rust-codes's Issues

「第十三章」代码清单13-49 #[no_std]上下文不能使用println!宏

页码与行数

  • 第513页
  • 代码清单13-49

代码错误

#![no_std]
fn main() {
    println!("Hello, world!"); // 此行代码应去掉或换一个例子
    //因为println!宏是std包提供的,不能在no_std上下文使用
}

Rust版本

C:\Users\洛佳>rustc -V
rustc 1.33.0-nightly (9eac38634 2018-12-31)

错误信息

   Compiling playground v0.0.1 (/playground)
error: cannot find macro `println!` in this scope
 --> src/main.rs:3:5
  |
3 |     println!("Hello, world!");
  |     ^^^^^^^

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

「第二章」 阅读建议

阅读过程中的一些建议和疑问:

  • 20页
    介绍 CTFE 部分,不用提 const generics 部分。因为既然还在实现,另外读者也刚接触 Rust,对 impl 那个代码片段会一脸懵逼。

  • 21 页
    闭包作为参数部分,建议放到后面闭包的章节,因为对于刚接触的新手来说还未读到类型及范型,这样的代码示例也会稍难理解。

  • 23 页
    while_true 返回『单元值』,这个新手会不太好理解,而且后来的类型部分也未介绍单元值,其实对于之前未接触过类型安全型语言的朋友应该是不太好理解,这一特殊类型。我虽然知道是 '()',但是之前不清楚原来这在中文中叫作"单元值"。

「第三章」读者建议:普遍性到特殊性的讲解过程有改进空间

来自Gabin

版权归作者所有,任何形式转载请联系作者。
作者:gabin(来自豆瓣)
来源:https://book.douban.com/review/9884812/

。从普遍性到特殊性的讲解过程,有时候读起来也费事。比如,3.3节(page 60)讲解泛型的时候,一开始就给出泛型的代码示例,幸好我有跳读的习惯,在读到61页的代码清单3-20的时候,才明白泛型函数经过编译后会变成多个具体类型的函数。那么按照问题求解的思路,首先我们可以先把3-20的代码写出来,然后说“两个函数仅仅是类型定义上的区别,为了这点区别就写多个函数太费事了,应该提供一种类型抽象功能,让程序员可以写一个函数就搞定”,然后再贴出3-16的代码示例。可见,从特殊到普遍也符合人类的认知发展过程。希望作者能在新版中做出这类改进,让读者能更顺畅地阅读。

「第六章」函数指针类型的一点建议

页码与行数

  • 第169页
  • 第10行

书中在这里讲到 let other_fn = hello; 这里的类型是fn() {hello} 是这个函数本身的类型而不是函数指针类型, 后面还说到, “传入sum和product函数名之后, 会自动通过模式匹配转换为函数指针类型”

读到这里的时候, 有点不太理解 “函数本身的类型” 和 函数指针类型在rust内的具体区别, 以及, 是哪个trait/语言特性导致了 函数类型和函数指针的相互转换?

函数调用的时候如果传入一个函数名, 到底传入的是什么? 这里的细节还希望作者可以能多深入写两句. 因为写了函数本身的类型后, 我读后面的内容老是会纠结这两个概念在每个地方实际上是什么样子的.

仅仅是个人建议~

本书是否有计划跟进Rust 2018 edition

社区消息显示Rust 2018 edition首个正式版本rust 1.31将于12.6发布。

相对于Rust 2015 edition,编译器的使用和语言特性均有不小变化,本书是否有计划跟进Rust 2018 edition,还是准备在第二版中在进行跟进,谢谢!

「第二章」增补源码编码描述

2.6.3字符类型 中,可在介绍 Rust 的 char 类型的同时,也同时介绍 Rust 的源代码文件 .rs 文件的默认文本编码格式为 utf-8 ,且不支持其他文本编码格式

「第三章」建议介绍一下 MIR 的使用方式

我目前只看到第三章,不知道这个建议有没有在后面章节做了。

MIR 一定程度上表示了 “源代码在编译器眼中是什么样子的”。所以我为了查看 MIR,找了很久,最后才(只)知道可以通过 https://play.rust-lang.org 来查看。

我认为了解 MIR ,这种从编译器的视角去看代码的方式,能帮助我们更深刻地了解 Rust 的心智模型。所以建议书中介绍一下,怎么查看 MIR。

另外,在 P37 中介绍枚举体 “这样的枚举值本质上属于函数指针类型”。我第一次知道这个知识点时是有点震惊的,而如果能此时够贴上相应的 MIR,相信更能解释这个知识点。
如果在这个知识点下面,再继续普及一下 constructors-as-functions ,就更好了。

「第十三章」 `13.2.2` 节 `子类型与型变` 中的错误

页码与行数

  • 第 492 页
  • 第 11 行

文本或排版错误

Fn(T) -> U 在 T 上是不变,在 U 上是协变。

按照 我测试的结果FnT 上应该是逆变的。

同理,第 494 页第二行,“普通的函数调用,参数是不变”,但实际上也是逆变的:一个类型为 &'a str 的形参永远可以传一个 &'static str 的实参进去。

PS:感觉这里实际上应该用 fn(T) -> Ufn 类型比 Fn trait 更加基础重要一些。
PS2:493 页就写了 PhantomData<fn(T)>T 上是逆变 😂

「第一章」考虑修改「Rust是跨平台语言,一次编译,到处运行」这个说法

页码与行数

  • 第一章
  • 第10页
  • 第3行

原文:

在进一步学习之前,我们有必要了解一下Rust 代码是如何执行的。Rust 是跨平台语言,
一次编译,到处运行,这得益于LLVM。Rust 编译器是一个LLVM 编译前端,它将代码编译
为LLVM IR,然后经过LLVM 编译为相应的平台目标。

说的好像Java一样。。重新考虑一下描述

「奇技淫巧」利用断言进行编译器优化

比如有个函数foo

fn foo(a: &[u8]) -> u32 {
     a[0] as u32 +
    (a[1] as u32 << 8) +
    (a[2] as u32 << 16) +
    (a[3] as u32 << 24)
}

这个函数如果你去翻汇编代码的话会发现它有四次unwrap判断(来自slice的直接索引)

然而你如果在这个函数开头加上 assert(a.len() >= 4) 就会变成只有一次检查了

考虑是否新增到第二版印刷中

「第九章」文本语义不通顺

页码与行数

  • 第318页
  • 最后一行

文本或排版错误

在代码清单 9-39 中,通过 From 为 Error 实现转换到 std::io::Error 和 std::num::ParseIntError

不知道是不是我理解有问题, 我觉得应该是 通过为Error实现From,将std::io::Error和std::num::ParseIntError转换为Error, 这样看起来更通顺。

看完了第九章的样章,张老师写得很好,书已经下单。

ps:也感谢张老师的Rust每日新闻频道,收获很多。

「修改封面」Steve离职,需要修改推荐序署名和封面

Steve今天已经从Rust团队离职。感谢他为我的书作序。

Steve今天在他的博客上宣布离开Mozilla。Steve说自己喜欢Rust,认为它是这个世界需要的语言,他愿意花自己的精力去推广。但可能在Mozilla工作这么多年,积累了很多委屈,让他越来越沮丧。而且最近,他发现自己在Rust团队的工资是最低的,当他试图表达自己的委屈时,却受到了纪律处分。所以,造成了这次离职。

Steve在Mozilla工作了大概6年,为Rust的推广做了很多工作,祝愿他一切顺利。Read More

「第四章」增补 `4.1` 节 `通用概念` 中的限定

因为 4.1 节中讲述的大量内容仅适用于 x86 平台,建议添加“以下以 x86 为例,简单的说明通用处理器平台的虚拟内存系统”等字样,以免混淆(MIPS 的虚拟地址空间管理很不相同;其他 ISA 中也不存在 ESP / EBP 这样的寄存器;等等)

「第十章」过期版本信息

页码与行数

  • 第322页
  • 第42行

文本或排版错误

使用cargo new 命令默认创建的是库文件(生成静态或动态链接库),它并非可执行文件,而是专门用于被其他应用程序共享的功能模块。如果想创建可执行文件,那么需要使用--bin参数。

现版本cargo new命令默认创建可执行文件,如果想创建库文件,需要使用--lib参数。

「第一章」提到GC的“世界暂停”,需要加个注解

第一章讲实践性的时候,提到“世界暂停”,有些读者可能不太明白这个意思。所以需要加个注解。

注:在几乎所有的GC实现中,都需要一个短暂的“整个程序世界暂停”阶段来扫描堆栈上的引用,以便安全地回收内存。

使用勘误模板

页码与行数

  • 第1页
  • 第2行

文本或排版错误

暂无

代码错误

let a = "hello";  // 此处应该是hello world

「第一章」锈菌是真菌,不是细菌

页码与行数

  • 第2页
  • 第9行

文本或排版错误

像不像一个细菌?

锈菌是一种真菌,而真菌是真核生物,细菌是原核生物。实际上,真菌和动物的亲缘关系比他们和植物的更加接近。

「第三章」 `3.1.3` 节 `类型系统与多态性` 中的描述错误

页码与行数

  • 第53页
  • 第19~21行

概念错误

3.1.3类型系统与多态性

而 Rust 语言重并没有类似 Java 中的继承的概念,所以也不存在子类型多态。所以,Rust 中的类型系统目前只支持参数化多态和 Ad-hoc 多态,也就是,泛型和 trait 。

实际上 Rust 支持 Subtyping ,但仅限于 lifetime 带来的 Subtyping 这一种情况。基于 lifetime 的 Subtyping 直接影响了 Rust 的 Variant & Covariant 规则。

可参见:Rust nomicon: Subtyping and Variance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.