Merge remote-tracking branch 'upstream/main'

pull/130/head
lijinpeng 4 years ago
commit a9c2e13508

@ -68,9 +68,9 @@
- [Box<T>堆对象分配](advance/smart-pointer/box.md) - [Box<T>堆对象分配](advance/smart-pointer/box.md)
- [Deref解引用](advance/smart-pointer/deref.md) - [Deref解引用](advance/smart-pointer/deref.md)
- [Drop释放资源](advance/smart-pointer/drop.md) - [Drop释放资源](advance/smart-pointer/drop.md)
- [Cell与RefCell todo](advance/smart-pointer/cell.md) - [Rc与Arc实现1vN所有权机制](advance/smart-pointer/rc-arc.md)
- [Rc与Arc todo](advance/smart-pointer/rc-refcell.md) - [Cell与RefCell内部可变性](advance/smart-pointer/cell-refcell.md)
- [自引用与内存泄漏 todo](advance/smart-pointer/self-referrence.md) - [Weak与自引用类型 todo](advance/smart-pointer/self-referrence.md)
- [全局变量 todo](advance/global-variable.md) - [全局变量 todo](advance/global-variable.md)
- [多线程 todo](advance/multi-threads/intro.md) - [多线程 todo](advance/multi-threads/intro.md)
- [线程管理 todo](advance/multi-threads/thread.md) - [线程管理 todo](advance/multi-threads/thread.md)
@ -85,7 +85,7 @@
- [一些写代码的技巧 todo](practice/coding-tips.md) - [一些写代码的技巧 todo](practice/coding-tips.md)
- [最佳实践 todo](practice/best-pratice.md) - [最佳实践 todo](practice/best-pratice.md)
- [Rust陷阱系列](pitfalls/index.md) - [Rust陷阱系列(持续更新)](pitfalls/index.md)
- [for循环中使用外部数组](pitfalls/use-vec-in-for.md) - [for循环中使用外部数组](pitfalls/use-vec-in-for.md)
- [线程类型导致的栈溢出](pitfalls/stack-overflow.md) - [线程类型导致的栈溢出](pitfalls/stack-overflow.md)
- [算术溢出导致的panic](pitfalls/arithmetic-overflow.md) - [算术溢出导致的panic](pitfalls/arithmetic-overflow.md)
@ -94,9 +94,10 @@
- [可变借用失败引发的深入思考](pitfalls/multiple-mutable-references.md) - [可变借用失败引发的深入思考](pitfalls/multiple-mutable-references.md)
- [不太勤快的迭代器](pitfalls/lazy-iterators.md) - [不太勤快的迭代器](pitfalls/lazy-iterators.md)
- [奇怪的序列x..y](pitfalls/weird-ranges.md) - [奇怪的序列x..y](pitfalls/weird-ranges.md)
- [无处不在的迭代器](pitfalls/iterator-everywhere.md)
- [编译期报错的RefCell todo](pitfalls/refcell-compilation-error.md)
- [对抗编译检查(持续更新)](fight-with-compiler/intro.md)
- [对抗编译检查 doing](fight-with-compiler/intro.md)
- [幽灵数据(todo)](fight-with-compiler/phantom-data.md) - [幽灵数据(todo)](fight-with-compiler/phantom-data.md)
- [生命周期)](fight-with-compiler/lifetime/intro.md) - [生命周期)](fight-with-compiler/lifetime/intro.md)
- [生命周期过大-01](fight-with-compiler/lifetime/too-long1.md) - [生命周期过大-01](fight-with-compiler/lifetime/too-long1.md)

@ -94,7 +94,8 @@ fn main() {
``` ```
该段代码不能通过编译的原因,是因为某个借用不再需要,但编译器不理解这点,反而谨慎的给该借用安排了一个很大的作用域,结果导致后续的借用失败: 这段代码不能通过编译的原因是编译器未能精确地判断出某个可变借用不再需要,反而谨慎的给该借用安排了一个很大的作用域,结果导致后续的借用失败:
```console ```console
error[E0499]: cannot borrow `*map` as mutable more than once at a time error[E0499]: cannot borrow `*map` as mutable more than once at a time
--> src/main.rs:13:17 --> src/main.rs:13:17
@ -116,14 +117,15 @@ error[E0499]: cannot borrow `*map` as mutable more than once at a time
| |_________- returning this value requires that `*map` is borrowed for `'m` | |_________- returning this value requires that `*map` is borrowed for `'m`
``` ```
可以看出,在`match map.get_mut(&key)`方法调用完成后,对`map`的可变借用就结束了,但是由于编译器不太聪明,它认为该借用会持续到整个`match`语句块的结束(第16行处)结果导致了后续借用的失败。 分析代码可知在`match map.get_mut(&key)`方法调用完成后,对`map`的可变借用就可以结束了。但从报错看来,编译器不太聪明,它认为该借用会持续到整个`match`语句块的结束(第16行处)这便造成了后续借用的失败。
类似的例子还有很多,由于篇幅有限,就不在这里一一列举,如果大家想要阅读更多的类似代码,可以看看[<<Rust>>](https://github.com/sunface/rust-codes)一书。 类似的例子还有很多,由于篇幅有限,就不在这里一一列举,如果大家想要阅读更多的类似代码,可以看看[<<Rust>>](https://github.com/sunface/rust-codes)一书。
## 无界生命周期 ## 无界生命周期
不安全代码(`unsafe`)经常会凭空产生引用或生命周期, 这些生命周期被称为是 **无界\(unbound\)** 的。
不安全代码(`unsafe`)经常会凭空产生引用或生命周期, 这些生命周期被称为是 **无界(unbound)** 的。
无界生命周期往往是在解引用一个原生指针(裸指针raw pointer)时产生的,换句话说,它是凭空产生的,因为输入参数根本就没有这个生命周期: 无界生命周期往往是在解引用一个原生指针(裸指针raw pointer)时产生的,换句话说,它是凭空产生的,因为输入参数根本就没有这个生命周期:
```rust ```rust
@ -226,7 +228,7 @@ let closure_slision = |x: &i32| -> &i32 { x };
编译器就必须深入到闭包函数体中,去分析和推测生命周期,复杂度因此极具提升:试想一下,编译器该如何从复杂的上下文中分析出参数引用的生命周期和闭包体中生命周期的关系? 编译器就必须深入到闭包函数体中,去分析和推测生命周期,复杂度因此极具提升:试想一下,编译器该如何从复杂的上下文中分析出参数引用的生命周期和闭包体中生命周期的关系?
由于上述原因(当然,实际情况复杂的多)Rust语言开发者其实目前是有意为之,针对函数和闭包实现了两种不同的生命周期消除规则。 由于上述原因(当然,实际情况复杂的多)Rust语言开发者目前其实是有意针对函数和闭包实现了两种不同的生命周期消除规则。
## NLL(Non-Lexical Lifetime) ## NLL(Non-Lexical Lifetime)
@ -312,7 +314,7 @@ struct Ref<'a, T> {
} }
``` ```
在本节的生命周期约束中也提到过新版本Rust中上面情况中的`T: 'a`可以被消除掉当然你也可以显式的声明但是会影响代码可读性。关于类似的场景Rust团队计划在未来提供更多的消除规则但是你懂,计划未来就等于未知。 在本节的生命周期约束中也提到过新版本Rust中上面情况中的`T: 'a`可以被消除掉当然你也可以显式的声明但是会影响代码可读性。关于类似的场景Rust团队计划在未来提供更多的消除规则但是你懂,计划未来就等于未知。
## 一个复杂的例子 ## 一个复杂的例子
下面是一个关于生命周期声明过大的例子,会较为复杂,希望大家能细细阅读,它能帮你对生命周期的理解更加深入。 下面是一个关于生命周期声明过大的例子,会较为复杂,希望大家能细细阅读,它能帮你对生命周期的理解更加深入。

@ -0,0 +1,355 @@
# Cell和RefCell
Rust的编译器之严格可以说是举世无双。特别是在所有权方面Rust通过严格的规则来保证所有权和借用的正确性最终为程序的安全保驾护航。
但是严格是一把双刃剑带来安全提升的同时损失了灵活性有时甚至会让用户痛苦不堪、怨声载道。因此Rust提供了`Cell`和`RefCell`用于内部可变性, 简而言之,可以在拥有不可变引用的同时修改目标数据,对于正常的代码实现来说,这个是不可能做到的(要么一个可变借用,要么多个不可变借用).
> 内部可变性的实现是因为Rust使用了`unsafe`来做到这一点但是对于使用者来说这些都是透明的因为这些不安全代码都被封装到了安全的API中
## Cell
Cell和RefCell在功能上没有区别区别在于`Cell<T>`适用于`T`实现`Copy`的情况:
```rust
use std::cell::Cell;
fn main() {
let c = Cell::new("asdf");
let one = c.get();
c.set("qwer");
let two = c.get();
println!("{},{}", one,two);
}
```
以上代码展示了`Cell`的基本用法,有几点值得注意:
- "asdf"是`&str`类型,它实现了`Copy`特征
- `c.get`用来取值,`c.set`用来设置新值
取到值保存在`one`变量后还能同时进行修改这个违背了Rust的借用规则但是由于`Cell`的存在,我们很优雅的做到了这一点,但是如果你尝试在`Cell`中存放`String`
```rust
let c = Cell::new(String::from("asdf"));
```
编译器会立刻报错,因为`String`没有实现`Copy`特征:
```console
| pub struct String {
| ----------------- doesn't satisfy `String: Copy`
|
= note: the following trait bounds were not satisfied:
`String: Copy`
```
## RefCell
由于`Cell`类型针对的是实现了`Copy`特征的值类型,因此在实际开发中,`Cell`使用的并不多,因为我们要解决的往往是可变、不可变引用共存导致的问题,此时就需要借助于`RefCell`来达成目的。
我们可以将所有权、借用规则与这些智能指针做一个对比:
| Rust规则 | 智能指针带来的额外规则 |
|--------|-------------|
| 一个数据只有一个所有者| `Rc/Arc`让一个数据可以拥有多个所有者 |
| 要么多个不可变借用,要么一个可变借用 | `RefCell`实现编译期可变、不可变引用共存 |
| 违背规则导致**编译错误** | 违背规则导致**运行时`panic`** |
可以看出,`Rc/Arc`和`RefCell`合在一起解决了Rust中严苛的所有权和借用规则带来的某些场景下难使用的问题。但是它们并不是银弹例如`RefCell`实际上并没有解决可变引用和引用可以共存的问题,只是将报错从编译期推迟到运行时,从编译器错误变成了`panic`异常:
```rust
use std::cell::RefCell;
fn main() {
let s = RefCell::new(String::from("hello, world"));
let s1 = s.borrow();
let s2 = s.borrow_mut();
println!("{},{}",s1,s2);
}
```
上面代码在编译期不会报任何错误,你可以顺利运行程序:
```console
thread 'main' panicked at 'already borrowed: BorrowMutError', src/main.rs:6:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
但是依然会因为违背了借用规则导致了运行期`panic`,这非常像中国的天网,它也许会被罪犯蒙蔽一时,但是并不会被蒙蔽一世,任何导致安全风险的存在都将不能被容忍,法网恢恢,疏而不漏。
#### RefCell为何存在
相信肯定有读者有疑问了,这么做有任何意义吗?还不如在编译期报错,至少能提前发现问题,而且性能还更好。
存在即合理究其根因在于Rust编译期的**宁可错杀,绝不放过**的原则, 当编译器不能确定你的代码是否正确时,就统统会判定为错误,因此难免会导致一些误报。
而`RefCell`正是**用于你确信代码是正确的,而编译器却发生了误判时**。
对于大型的复杂程序,也可以选择使用`RefCell`来让事情简化。例如在Rust编译器的[`ctxt结构体`](https://github.com/rust-lang/rust/blob/620d1ee5346bee10ba7ce129b2e20d6e59f0377d/src/librustc/middle/ty.rs#L803-L987)中有大量的`RefCell`类型的`map`字段, 主要的原因是:这些`map`会被分散在各个地方的代码片段所广泛使用或修改。由于这种分散在各处的使用方式,导致了管理可变和不可变成为一件非常复杂的任务(甚至不可能),你很容易就碰到编译器抛出来的各种错误。而且`RefCell`的运行时错误在这种情况下也变得非常可爱:一旦有人做了不正确的使用,代码会`panic`,然后告诉我们哪些借用冲突了。
总之,当你确信编译器误报但不知道该如何解决时,或者你有一个引用类型,需要被四处使用和修改然后导致借用关系难以管理时,都可以优先考虑使用`RefCell`。
#### RefCell简单总结
- 与Cell用于可Copy的值不同RefCell用于引用
- RefCell只是将借用规则从编译期推迟到程序运行期并不能帮你绕过这个规则
- RefCell适用于编译期误报或者一个引用被在多个代码中使用、修改以至于难于管理借用关系时
- 使用`RefCell`时,违背借用规则会导致运行期的`panic`
## 选择`Cell`还是`RefCell`
根据本文的内容,我们可以大概总结下两者的区别:
- `Cell`只适用于`Copy`类型,用于提供值, 而`RefCell`用于提供引用
- `Cell`不会`panic`,而`RefCell`会
#### 性能比较
`Cell`没有额外的性能损耗,例如以下两段代码的性能其实是一致的:
```rust
// code snipet 1
let x = Cell::new(1);
let y = &x;
let z = &x;
x.set(2);
y.set(3);
z.set(4);
println!("{}", x.get());
// code snipet 2
let mut x = 1;
let y = &mut x;
let z = &mut x;
x = 2;
*y = 3;
*z = 4;
println!("{}", x;
```
虽然性能一致,但代码`1`拥有代码`2`不具有的优势:它能编译成功:)
与`Cell`的`zero cost`不同,`RefCell`其实是有一点运行期开销的,原因是它包含了一个字大小的"借用状态"指示器,该指示器在每次运行时借用时都会被修改,进而产生一点开销。
总之,当非要使用内部可变性时,首选`Cell`,只有值拷贝的方式不能满足你时,才去选择`RefCell`。
## 内部可变性
之前我们提到RefCell具有内部可变性何为内部可变性简单来说对一个不可变的值进行可变借用但这个并不符合Rust的基本借用规则
```rust
fn main() {
let x = 5;
let y = &mut x;
}
```
上面的代码会报错因为我们不能对一个不可变的值进行可变借用这会破坏Rust的安全性保证相反你可以对一个可变值进行不可变借用。原因是当值不可变时可能会有多个不可变的引用指向它修改其中一个为可变的会造成可变引用与不可变引用共存的情况而当值可变时只会有唯一一个可变引用指向它将其修改为不可变那么最终依然是只有一个不可变的引用指向它。
虽然基本借用规则是Rust的基石然而在某些场景中一个值可以在其方法内部被修改同时对于其它代码不可变是很有用的:
```rust
// 定义在外部库中的特征
pub trait Messenger {
fn send(&self, msg: String);
}
// --------------------------
// 我们的代码中的数据结构和实现
struct MsgQueue {
msg_cache: Vec<String>,
}
impl Messenger for MsgQueue {
fn send(&self,msg: String) {
self.msg_cache.push(msg)
}
}
```
如上所示,外部库中定义了一个消息发送器特征`Messenger`,它就一个功能用于发送消息: `fn send(&self, msg: String)`,因为发送消息不需要修改自身,因此原作者在定义时,使用了`&self`的不可变借用, 这个无可厚非。
但是问题来了,我们要在自己的代码中使用该特征实现一个异步消息队列,出于性能的考虑,消息先写到本地缓存(内存)中,然后批量发送出去,因此在`send`方法中,需要将消息先行插入到本地缓存`msg_cache`中。但是问题来了,该`send`方法的签名是`&self`,因此上述代码会报错:
```console
error[E0596]: cannot borrow `self.sent_messages` as mutable, as it is behind a `&` reference
--> src/main.rs:11:9
|
2 | fn send(&self, msg: String);
| ----- help: consider changing that to be a mutable reference: `&mut self`
...
11 | self.sent_messages.push(msg)
| ^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
```
在报错的同时,编译器大聪明还善意的给出了提示:将`&self`修改为`&mut self`,但是。。。我们实现的特征是定义在外部库中,因此该签名根本不能修改。值此危急关头,`RefCell`闪亮登场:
```rust
use std::cell::RefCell;
pub trait Messenger {
fn send(&self, msg: String);
}
pub struct MsgQueue {
msg_cache: RefCell<Vec<String>>,
}
impl Messenger for MsgQueue {
fn send(&self,msg: String) {
self.msg_cache.borrow_mut().push(msg)
}
}
fn main() {
let mq = MsgQueue{msg_cache: RefCell::new(Vec::new())};
mq.send("hello, world".to_string());
}
```
这个MQ功能很弱但是并不妨碍我们演示内部可变性的核心用法通过包裹一层`RefCell`,成功的让`&self`中的`msg_cache`成为一个可变值,然后实现对其的修改。
## Rc + RefCell组合使用
在Rust中一个常见的组合就是`Rc`和`RefCell`在一起使用,前者可以实现一个数据拥有多个所有者,后者可以实现数据的可变性:
```rust
use std::cell::RefCell;
use std::rc::Rc;
fn main() {
let s = Rc::new(RefCell::new("我很善变,还拥有多个主人".to_string()));
let s1 = s.clone();
let s2 = s.clone();
// let mut s2 = .borrow_mut();
s2.borrow_mut().push_str(", on yeah!");
println!("{:?}\n{:?}\n{:?}", s, s1, s2);
}
```
上面代码中,我们使用`RefCell<String>`包裹一个字符串,同时通过`Rc`创建了它的三个所有者:`s`,`s1`和`s2`,并且通过其中一个所有者`s2`对字符串内容进行了修改。
由于`Rc`的所有者们共享同一个底层的数据,因此当一个所有者修改了数据时,会导致全部所有者持有的数据都发生了变化。
程序的运行结果也在预料之中:
```console
RefCell { value: "我很善变,还拥有多个主人, on yeah!" }
RefCell { value: "我很善变,还拥有多个主人, on yeah!" }
RefCell { value: "我很善变,还拥有多个主人, on yeah!" }
```
#### 性能损耗
相信这两者组合在一起使用时,很多人会好奇到底性能如何,下面我们来简单分析下。
首先给出一个大概的结论这两者结合在一起使用的性能其实非常高大致相当于没有线程安全版本的C++ `std::shared_ptr`指针, 事实上,`C++`这个指针的主要开销也在于原子性这个并发原语上,毕竟线程安全在哪个语言中开销都不小。
#### 内存损耗
两者结合的数据结构类似:
```rust
struct Wrapper<T> {
// Rc
strong_count: usize,
weak_count: usize,
// Refcell
borrow_count: isize,
// 包裹的数据
item: T,
}
```
从上面可以看出,从对内存的影响来看,仅仅多分配了三个`usize/isize`,并没有其它额外的负担。
#### CPU损耗
从CPU来看损耗如下
- 对`Rc<T>`解引用是免费的(编译期), 但是*带来的间接取值并不免费
- 克隆`Rc<T>`需要将当前的引用计数跟`0`和`usize::Max`进行一次比较然后将计数值加1
- 释放(drop)`Rc<T>`将计数值减1 然后跟`0`进行一次比较
- 对`RefCell`进行不可变借用,将`isize`类型的借用计数加1然后跟`0`进行比较
- 对`RefCell`的不可变借用进行释放,将`isize`减1
- 对`RefCell`的可变借用大致流程跟上面差不多,但是是先跟`0`比较然后再减1
- 对`RefCell`的可变借用进行释放,将`isize`加1
其实这些细节不必过于关注,只要知道`CPU`消耗也非常低,甚至编译器还会对此进行进一步优化!
#### CPU缓存Miss
唯一需要担心的可能就是这种组合数据结构对于`CPU`缓存是否亲和,这个我们无法证明,只能提出来存在这个可能性,最终的性能影响还需要在实际场景中进行测试
总之,分析这两者组合的性能还挺复杂的,大概总结下:
- 从表面来看它们带来的内存和CPU损耗都不大
- 但是由于`Rc`额外的引入了一次间接取值(*),在少数场景下可能会造成性能上的显著损失
- CPU缓存可能也不够亲和
## 通过`Cell::from_mut`解决借用冲突
在Rust1.37版本中新增了两个非常实用的方法:
- Cell::from_mut, 该方法将`&mut T`转为`&Cell<T>`
- Cell::as_slice_of_cells该方法将`&Cell<[T]>`转为`&[Cell<T>]`
这里我们不做深入的介绍,但是来看看如何使用这两个方法来解决一个常见的借用冲突问题:
```rust
fn is_even(i: i32) -> bool {
i % 2 == 0
}
fn retain_even(nums: &mut Vec<i32>) {
let mut i = 0;
for num in nums.iter().filter(|&num| is_even(*num)) {
nums[i] = *num;
i += 1;
}
nums.truncate(i);
}
```
以上代码会报错:
```console
error[E0502]: cannot borrow `*nums` as mutable because it is also borrowed as immutable
--> src/main.rs:8:9
|
7 | for num in nums.iter().filter(|&num| is_even(*num)) {
| ----------------------------------------
| |
| immutable borrow occurs here
| immutable borrow later used here
8 | nums[i] = *num;
| ^^^^ mutable borrow occurs here
```
很明显,因为同时借用了不可变与可变引用,你可以通过索引的方式来绕过:
```rust
fn retain_even(nums: &mut Vec<i32>) {
let mut i = 0;
for j in 0..nums.len() {
if is_even(nums[j]) {
nums[i] = nums[j];
i += 1;
}
}
nums.truncate(i);
}
```
但是这样就违背我们的初衷了,而且迭代器会让代码更加简洁,还有其它的办法吗?
这时就可以使用`Cell`新增的这两个方法:
```rust
use std::cell::Cell;
fn retain_even(nums: &mut Vec<i32>) {
let slice: &[Cell<i32>] = Cell::from_mut(&mut nums[..])
.as_slice_of_cells();
let mut i = 0;
for num in slice.iter().filter(|num| is_even(num.get())) {
slice[i].set(num.get());
i += 1;
}
nums.truncate(i);
}
```
此时代码将不会报错,因为`Cell`上的`set`方法获取的是不可变引用`pub fn set(&self, val: T) {`.
当然,以上代码的本质还是对`Cell`的运用,只不过这两个方法可以很方便的帮我们把`&mut T`类型转换成`&[Cell<T>]`类型。
## 总结
`Cell`和`RefCell`都为我们带来了内部可见性这个重要特性,同时还将借用规则的检查从编译期推迟到运行期,但是这个检查并不能被绕过,该来早晚还是会来,`R在运行期的报错会造成`panic`
`RefCell`适用于编译器误报或者一个引用被在多个代码中使用、修改以至于难于管理借用关系时,还有就是需要内部可变性时。
从性能上看,`RefCell`由于是非线程安全的,因此无需保证原子性,性能虽然有一点损耗,但是依然非常好,而`Cell`则完全不存在任何额外的性能损耗。
`Rc`跟`RefCell`结合使用可以实现多个所有者共享同一份数据,非常好用,但是潜在的性能损耗也要考虑进去,建议对于热点代码使用时,做好`benchmark`.

@ -1,4 +0,0 @@
# Cell todo
https://ryhl.io/blog/temporary-shared-mutation/

@ -0,0 +1,201 @@
# Rc与Arc
Rust所有权机制要求一个值只能有一个所有者在大多数情况下都没有问题但是考虑以下情况:
- 在图数据结构中,多个边可能会拥有同一个节点,该节点直到没有边指向它时,才应该被释放清理
- 在多线程中多个线程可能会指向同一个数据但是你受限于Rust的安全机制你又无法同时的可变借用该数据
以上场景不是很常见但是一旦遇到就非常棘手为了解决此类问题Rust在所有权机制之外又引入了额外的措施来简化相应的实现通过引用计数的方式允许一个数据资源在同一时刻拥有多个所有者。
这种实现机制就是`Rc`和`Arc`,前者适用于单线程,后者适用于多线程。由于二者大部分时间都是相同,因此本章将以`Rc`作为讲解主体,对于`Arc`的不同之处,也将进行单独讲解。
## Rc<T>
引用计数(reference counting),顾名思义,通过记录一个数据被引用的次数来确定该数据是否正在被使用。当引用次数归零时,就代表该数据不再被使用,因此可以被清理释放。
而`Rc`正是引用计数的英文缩写。当我们**希望在堆上分配一个对象供程序的多个部分使用且无法确定哪个部分最后一个结束时,就可以使用`Rc`成为数据值的所有者**,例如之前提到的多线程场景就非常适合。
下面是经典的所有权被转移导致报错的例子:
```rust
fn main() {
let s = String::from("hello, world");
// s在这里被转移给a
let a = Box::new(s);
// 报错此处继续尝试将s转移给b
let b = Box::new(s);
}
```
使用`Rc`就可以轻易解决:
```rust
use std::rc::Rc;
fn main() {
let a = Rc::new(String::from("hello, world"));
let b = Rc::clone(&a);
assert_eq!(2, Rc::strong_count(&a));
assert_eq!(Rc::strong_count(&a),Rc::strong_count(&b))
}
```
以上代码我们使用`Rc::new`创建了一个新的`Rc<String>`智能指针并赋给变量`a`,该指针指向底层的字符串数据。
智能指针`Rc<T>`在创建时还会将引用计数加1此时获取引用计数的关联函数`Rc::strong_count`返回的值将是`1`。
#### Rc::clone
接着,我们又使用`Rc::clone`克隆了一份智能指针`Rc<String>`,并将该智能指针的引用计数增加到`2`。
由于`a`和`b`是同一个智能指针的两个副本,因此通过它们两个获取引用计数的结果都是`2`。
不要给`clone`字样所迷惑,以为所有的`clone`都是深拷贝。这里的`clone`**仅仅复制了智能指针并增加了引用计数,并没有克隆底层数据**,因此`a`和`b`是共享了底层的字符串`s`,这种**复制效率是非常高**的。当然你也可以使用`a.clone()`的方式来克隆,但是从可读性角度,`Rc::clone`的方式我们更加推荐。
实际上Rust中还有不少`clone`都是浅拷贝,例如[迭代器的克隆](https://zhuanlan.zhihu.com/p/453149727).
#### 观察引用计数的变化
使用关联函数`Rc::strong_count`可以获取当前引用计数的值,我们来观察下引用计数如何随着变量声明、释放而变化:
```rust
use std::rc::Rc;
fn main() {
let a = Rc::new(String::from("test ref counting"));
println!("count after creating a = {}", Rc::strong_count(&a));
let b = Rc::clone(&a);
println!("count after creating b = {}", Rc::strong_count(&a));
{
let c = Rc::clone(&a);
println!("count after creating c = {}", Rc::strong_count(&c));
}
println!("count after c goes out of scope = {}", Rc::strong_count(&a));
}
```
有几点值得注意:
- 由于变量`c`在语句块内部声明当离开语句块时它会因为超出作用域而被释放最终引用计数会减少1, 事实上这个得益于`Rc<T>`实现了`Drop`特征
- `a`,`b`,`c`三个智能指针引用计数都是同样的,并且共享底层的数据,因此打印计数时用哪个都行
- 无法看到的是: 当`a`、`b`超出作用域后引用计数会变成0最终智能指针和它指向的底层字符串都会被清理释放
#### 不可变引用
事实上,`Rc<T>`是指向底层数据的不可变的引用因此你无法通过它来修改数据这也符合Rust的借用规则要么多个不可变借用要么一个可变借用。
但是可以修改数据也是非常有用的,只不过我们需要配合其它数据类型来一起使用,例如内部可变性的`RefCell<T>`类型以及互斥锁`Mutex<T>`。事实上,在多线程编程中,`Arc`跟`Mutext`锁的组合使用非常常见,既可以让我们在不同的线程中共享数据,又允许在各个线程中对其进行修改。
#### 一个综合例子
考虑一个场景,有很多小器具,里面每个器具都有自己的主人,但是存在多个器具属于同一个主人的情况,此时使用`Rc<T>`就非常适合:
```rust
use std::rc::Rc;
struct Owner {
name: String,
// ...other fields
}
struct Gadget {
id: i32,
owner: Rc<Owner>,
// ...other fields
}
fn main() {
// 创建一个基于引用计数的`Owner`.
let gadget_owner: Rc<Owner> = Rc::new(
Owner {
name: "Gadget Man".to_string(),
}
);
// 创建两个不同的工具,它们属于同一个主人
let gadget1 = Gadget {
id: 1,
owner: Rc::clone(&gadget_owner),
};
let gadget2 = Gadget {
id: 2,
owner: Rc::clone(&gadget_owner),
};
// 释放掉第一个`Rc<Owner>`
drop(gadget_owner);
// 尽管在之前我们释放了gadget_owner但是依然可以在这里使用owner的信息
// 原因是上面仅仅drop掉其中一个智能指针引用而不是drop掉owner数据外面还有两个引用指向底层的owner数据引用计数尚未清零
// 因此owner数据依然可以被使用
println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
println!("Gadget {} owned by {}", gadget2.id, gadget2.owner.name);
// 在函数最后,`gadget1`和`gadget2`也被释放,最终引用计数归零,随后底层数据也被清理释放
}
```
以上代码很好的展示了`Rc<T>`的用途,当然你也可以用借用的方式,但是实现起来就会复杂的多,而且随着工具在各个代码中的四处使用,引用生命周期也将变得更加复杂,毕竟结构体中的引用类型,总是令人不那么愉快,对不?
#### Rc简单总结
- `Rc/Arc`是不可变引用,你无法修改它指向的值,只能进行读取, 如果要修改,需要配合后面章节的内部可变性`RefCell`或互斥锁`Mutex`
- 一旦最后一个拥有者消失,则资源会自动被回收,这个生命周期是在编译期就确定下来的
- Rc只能用于同一线程内部想要用于线程之间的对象共享, 你需要使用`Arc`
- `Rc<T>`是一个智能指针,实现了`Deref`特征,因此你无需先解开`Rc`指针,再使用里面的`T`,而是可以直接使用`T`, 例如上例中的`gadget1.owner.name`
## 多线程无力的Rc<T>
来看看在多线程场景使用`Rc<T>`会如何:
```rust
use std::rc::Rc;
use std::thread;
fn main() {
let s = Rc::new(String::from("多线程漫游者"));
for _ in 0..10 {
let s = Rc::clone(&s);
let handle = thread::spawn(move || {
println!("{}",s)
});
}
}
```
由于我们还没有学习多线程的章节,上面的例子就特地简化了相关的实现。首先通过`thread::spawn`创建一个线程,然后使用`move`关键字把克隆出的`s`的所有权转移到线程中。
能够实现这一点,完全得益于`Rc`带来的多所有权机制,但是以上代码会报错:
```console
error[E0277]: `Rc<String>` cannot be sent between threads safely
```
表面原因是`Rc<T>`不能在线程间安全的传递,实际上是因为它没有实现`Send`特征,而该特征是恰恰是多线程间传递数据的关键,我们会在多线程章节中进行讲解。
当然,还有更深层的原因: 由于`Rc<T>`需要管理引用计数,但是该计数器并没有使用任何并发原语,因此无法实现原子化的计数操作, 最终会导致计数错误。
好在天无绝人之路一起来看看Rust为我们提供的功能一致但是多线程安全的`Arc`。
## Arc
`Arc`是`Atomic Rc`的缩写,顾名思义:原子化的`Rc<T>`智能指针。原子化是一种并发原语,我们在后续章节会进行深入讲解,这里你只要知道它能保证我们的数据能够安全的被线程间共享即可。
#### Arc的性能损耗
你可能好奇,为何不直接使用`Arc`,还要画蛇添足弄一个`Rc`还有Rust的基本数据类型、标准库数据类型为什么不自动实现原子化操作
原因在于原子化或者其它锁带来的线程安全都会伴随着性能损耗而且这种性能损耗还不小因此Rust把这种选择权交给你毕竟需要线程安全的代码其实占比并不高大部分时间我们都在跟线程内的代码执行打交道。
`Arc`和`Rc`拥有完全一样的API修改起来很简单:
```rust
use std::sync::Arc;
use std::thread;
fn main() {
let s = Arc::new(String::from("多线程漫游者"));
for _ in 0..10 {
let s = Arc::clone(&s);
let handle = thread::spawn(move || {
println!("{}",s)
});
}
}
```
对了,两者还有一点区别: `Arc`和`Rc`并没有定义在同一个模块,前者通过`use std::sync::Arc`来引入,后者`use std::rc::Rc`.
## 总结
在Rust中所有权机制保证了一个数据只会有一个所有者如果你想要在图数据结构、或者多线程等中使用这种机制会成为极大的阻碍。 好在Rust为我们提供了智能指针`Rc`和`Arc`,使用它们就能实现多个所有者共享一个数据的功能。
`Rc`和`Arc`的区别在于,后者是原子化实现的引用计数,因此是线程安全的,可以用于多线程中共享数据。
这两者都是只读的,如果想要实现内部数据可修改,必须配合内部可变性`RefCell`或者互斥锁`Mutex`来一起使用。

@ -1,3 +1,7 @@
# 优化编译速度 # 优化编译速度
https://www.reddit.com/r/rust/comments/rnkyc0/why_does_my_code_compile_faster_on_nightly/ https://www.reddit.com/r/rust/comments/rnkyc0/why_does_my_code_compile_faster_on_nightly/
https://www.reddit.com/r/rust/comments/rv8126/speedup_compilation_time/
https://www.reddit.com/r/rust/comments/rsfcgb/why_is_my_rust_build_so_slow/

@ -176,3 +176,7 @@ fn main() {
} }
``` ```
It takes 2.03 ms to execute :) It takes 2.03 ms to execute :)
## 动态和静态分发
https://www.reddit.com/r/rust/comments/ruavjm/is_there_a_difference_in_performance_between/

@ -1 +1,4 @@
# performance # performance
## How do I profile a Rust web application in production?
https://www.reddit.com/r/rust/comments/rupcux/how_do_i_profile_a_rust_web_application_in/

@ -27,3 +27,6 @@ https://www.reddit.com/r/rust/comments/rntx7s/why_use_boxleak/
## bounds check ## bounds check
https://www.reddit.com/r/rust/comments/rnbubh/whats_the_big_deal_with_bounds_checking/ https://www.reddit.com/r/rust/comments/rnbubh/whats_the_big_deal_with_bounds_checking/
## 使用assert 优化检查性能
https://www.reddit.com/r/rust/comments/rui1zz/write_assertions_that_clarify_code_to_both_the/

@ -0,0 +1,107 @@
# 无处不在的迭代器
Rust的迭代器无处不在直至你在它上面栽了跟头经过深入调查才发现原来是迭代器的锅。不信的话看看这个报错你能想到是迭代器的问题吗: `borrow of moved value: words`.
## 报错的代码
以下的代码非常简单,用来统计文本中字词的数量,并打印出来:
```rust
fn main() {
let s = "hello world";
let mut words = s.split(" ");
let n = words.count();
println!("{:?}",words);
}
```
四行代码,行云流水,一气呵成,且看成效:
```console
error[E0382]: borrow of moved value: `words`
--> src/main.rs:5:21
|
3 | let mut words = s.split(" ");
| --------- move occurs because `words` has type `std::str::Split<'_, &str>`, which does not implement the `Copy` trait
4 | let n = words.count();
| ------- `words` moved due to this method call
5 | println!("{:?}",words);
| ^^^^^ value borrowed here after move
```
世事难料,我以为只有的生命周期、闭包才容易背叛革命,没想到一个你浓眉大眼的`count`方法也背叛革命。从报错来看,是因为`count`方法拿走了`words`的所有权,来看看签名:
```rust
fn count(self) -> usize
```
从签名来看,编译器的报错是正确的,但是为什么?为什么一个简单的标准库`count`方法就敢拿走所有权?
## 迭代器回顾
在[迭代器](../advance/functional-programing/iterator.md#消费者与适配器)章节中,我们曾经学习过两个概念:迭代器适配器和消费者适配器,前者用于对迭代器中的元素进行操作,最终生成一个新的迭代器,例如`map`、`filter`等方法;而后者用于消费掉迭代器,最终产生一个结果,例如`collect`方法, 一个典型的示例如下:
```rust
let v1: Vec<i32> = vec![1, 2, 3];
let v2: Vec<_> = v1.iter().map(|x| x + 1).collect();
assert_eq!(v2, vec![2, 3, 4]);
```
在其中,我们还提到一个细节,消费者适配器会拿走迭代器的所有权,那么这个是否与我们最开始碰到的问题有关系?
## 深入调查
要解释这个问题,必须要找到`words`是消费者适配器的证据,因此我们需要深入源码进行查看。
其实。。也不需要多深,只要进入`words`的源码,就能看出它属于`Iterator`特征,那说明`split`方法产生了一个迭代器?再来看看:
```rust
pub fn split<'a, P>(&'a self, pat: P) -> Split<'a, P>
where
P: Pattern<'a>,
//An iterator over substrings of this string slice, separated by characters matched by a pattern.
```
还真是,从代码注释来看,`Split`就是一个迭代器类型,用来迭代被分隔符隔开的子字符串集合。
真相大白了,`split`产生一个迭代器,而`count`方法是一个消费者适配器,用于消耗掉前者产生的迭代器,最终生成字词统计的结果。
本身问题不复杂,但是在**解决方法上,可能还有点在各位客官的意料之外**,且看下文。
## 最rusty的解决方法
你可能会想用`collect`来解决这个问题,先收集成一个集合,然后进行统计。当然此方法完全可行,但是很不`rusty`(很符合rust规范、潮流的意思),以下给出最`rusty`的解决方案:
```rust
let words = s.split(",");
let n = words.clone().count();
```
在继续之前,我得先找一个地方藏好,因为俺有一个感觉,烂西红柿正在铺天盖地的呼啸而来,伴随而来的是读者的正义呵斥:
**你管`clone`叫最好、最`rusty`的解决方法??**
大家且听我慢慢道来事实上在Rust中`clone`不总是性能低下的代名词,因为`clone`的行为完全取决于它的具体实现。
#### 迭代器的`clone`代价
对于迭代器而言,它其实并不需要持有数据才能进行迭代,事实上它包含一个引用,该引用指向了保存在堆上的数据,而迭代器自身的结构是保存在栈上。
因此对迭代器的`clone`仅仅是复制了一份栈上的简单结构,性能非常高效,例如:
```rust
pub struct Split<'a, T: 'a, P>
where
P: FnMut(&T) -> bool,
{
// Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods
pub(crate) v: &'a [T],
pred: P,
// Used for `SplitAsciiWhitespace` `as_str` method
pub(crate) finished: bool,
}
impl<T, P> Clone for Split<'_, T, P>
where
P: Clone + FnMut(&T) -> bool,
{
fn clone(&self) -> Self {
Split { v: self.v, pred: self.pred.clone(), finished: self.finished }
}
}
```
以上代码实现了对`Split`迭代器的克隆,可以看出,底层的的数组`self.v`并没有被克隆而是简单的复制了一个引用,依然指向了底层的数组`&[T]`,因此这个克隆非常高效。
## 总结
看起来是无效借用导致的错误实际上是迭代器被消费了导致的问题这说明Rust编译器虽然会告诉你错误原因但是这个原因不总是根本原因。我们需要一双慧眼和勤劳的手来挖掘出这个宝藏最后为己所用。
同时克隆在Rust中也并不总是**bad guy**的代名词,有的时候我们可以大胆去使用,当然前提是了解你的代码场景和具体的`clone`实现,这样你也能像文中那样作出非常`rusty`的选择。

@ -0,0 +1,8 @@
# 编译期报错的RefCell
@todo
https://stackoverflow.com/questions/67023741/mutating-fields-of-rc-refcell-depending-on-its-other-internal-fields
https://stackoverflow.com/questions/47060266/error-while-trying-to-borrow-2-fields-from-a-struct-wrapped-in-refcell

@ -8,3 +8,6 @@ https://www.reddit.com/r/rust/comments/rrgho1/what_is_the_recommended_way_to_use
## 最佳开发流程 ## 最佳开发流程
cargo watch cargo watch
## 测试文件组织结构
https://www.reddit.com/r/rust/comments/rsuhnn/need_a_piece_of_advice_about_organising_tests/

@ -0,0 +1 @@
https://www.reddit.com/r/rust/comments/rtqrx4/introducing_atomicstory/

@ -25,3 +25,6 @@ fn main() {
} }
} }
``` ```
## 多个线程同时无锁的对一个数组进行修改
https://www.reddit.com/r/rust/comments/rtutr0/lockless_threads_for_mutable_operations/

@ -1,9 +1,8 @@
# Primitive Types # 基本类型(Primitive Types
Rust has a couple of basic types that are directly implemented into the Rust 有几个直接在编译器中实现基本类型。在本节中,我们来看看最重要的几个。
compiler. In this section, we'll go through the most important ones.
## Further information ## 更多信息
- [Data Types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html) - [Data Types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html)
- [The Slice Type](https://doc.rust-lang.org/stable/book/ch04-03-slices.html) - [The Slice Type](https://doc.rust-lang.org/stable/book/ch04-03-slices.html)

@ -1,6 +1,6 @@
// primitive_types1.rs // primitive_types1.rs
// Fill in the rest of the line that has code missing! // 补充不完整代码行的缺失部分!
// No hints, there's no tricks, just get used to typing these :) // 没有提示,也没有什么诀窍,只要习惯于键入这些内容就可以了 :)
// I AM NOT DONE // I AM NOT DONE
@ -9,11 +9,11 @@ fn main() {
let is_morning = true; let is_morning = true;
if is_morning { if is_morning {
println!("Good morning!"); println!("Good morning!");// 译:"早上好"
} }
let // Finish the rest of this line like the example! Or make it be false! let // 参照上面的示例来补充这一行的缺失部分!或者让它值为 false
if is_evening { if is_evening {
println!("Good evening!"); println!("Good evening!");// 译:"晚上好"
} }
} }

@ -1,6 +1,6 @@
// primitive_types2.rs // primitive_types2.rs
// Fill in the rest of the line that has code missing! // 补充不完整代码行的缺失部分!
// No hints, there's no tricks, just get used to typing these :) // 没有提示,也没有什么诀窍,只要习惯于键入这些内容就可以了 :)
// I AM NOT DONE // I AM NOT DONE
@ -9,16 +9,16 @@ fn main() {
let my_first_initial = 'C'; let my_first_initial = 'C';
if my_first_initial.is_alphabetic() { if my_first_initial.is_alphabetic() {
println!("Alphabetical!"); println!("Alphabetical!");// 译:"字母!"
} else if my_first_initial.is_numeric() { } else if my_first_initial.is_numeric() {
println!("Numerical!"); println!("Numerical!");// 译:"数字!"
} else { } else {
println!("Neither alphabetic nor numeric!"); println!("Neither alphabetic nor numeric!");// 译:"既不是字母也不是数字!"
} }
let // Finish this line like the example! What's your favorite character? let // 像上面的示例一样完成这行代码!你最喜欢的角色是什么?
// Try a letter, try a number, try a special character, try a character // 试试一个字母,或者一个数字,也可以一个特殊字符,又或者一个不属于
// from a different language than your own, try an emoji! // 你母语的字符,一个表情符号看起来也不错。
if your_character.is_alphabetic() { if your_character.is_alphabetic() {
println!("Alphabetical!"); println!("Alphabetical!");
} else if your_character.is_numeric() { } else if your_character.is_numeric() {

@ -1,6 +1,6 @@
// primitive_types3.rs // primitive_types3.rs
// Create an array with at least 100 elements in it where the ??? is. // 在 ??? 处创建一个不少于 100 个元素的数组。
// Execute `rustlings hint primitive_types3` for hints! // 执行 `rustex hint primitive_types3` 获取提示!
// I AM NOT DONE // I AM NOT DONE
@ -8,8 +8,8 @@ fn main() {
let a = ??? let a = ???
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");// 译:"哇!那数组可真大!"
} else { } else {
println!("Meh, I eat arrays like that for breakfast."); println!("Meh, I eat arrays like that for breakfast.");// 译:"嗯,我把这样的数组当早餐吃。"
} }
} }

@ -1,6 +1,6 @@
// primitive_types4.rs // primitive_types4.rs
// Get a slice out of Array a where the ??? is so that the test passes. // 在 ??? 处获取数组 a 的一个切片slice以通过测试。
// Execute `rustlings hint primitive_types4` for hints!! // 执行 `rustex hint primitive_types4` 获取提示!!
// I AM NOT DONE // I AM NOT DONE

@ -1,12 +1,12 @@
// primitive_types5.rs // primitive_types5.rs
// Destructure the `cat` tuple so that the println will work. // 对 `cat` 元组进行解构Destructure使 println 能够运行。
// Execute `rustlings hint primitive_types5` for hints! // 执行 `rustex hint primitive_types5` 获取提示!
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let cat = ("Furry McFurson", 3.5); let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat; let /* your pattern here */ = cat;// 译:模式写在这
println!("{} is {} years old.", name, age); println!("{} is {} years old.", name, age);
} }

@ -1,16 +1,16 @@
// primitive_types6.rs // primitive_types6.rs
// Use a tuple index to access the second element of `numbers`. // 使用元组索引tuple index来访问 `numbers` 的第二个元素。
// You can put the expression for the second element where ??? is so that the test passes. // 你可以把第二个元素的表达式放在 ??? 处,这样测试就会通过。
// Execute `rustlings hint primitive_types6` for hints! // 执行 `rustex hint primitive_types6` 获取提示!
// I AM NOT DONE // I AM NOT DONE
#[test] #[test]
fn indexing_tuple() { fn indexing_tuple() {
let numbers = (1, 2, 3); let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax. // 用元组索引的语法替换下面的 ???
let second = ???; let second = ???;
assert_eq!(2, second, assert_eq!(2, second,
"This is not the 2nd number in the tuple!") "This is not the 2nd number in the tuple!")// 译:这不是元组中的第二个数字!
} }

@ -212,40 +212,37 @@ https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-ref
name = "primitive_types1" name = "primitive_types1"
path = "exercises/primitive_types/primitive_types1.rs" path = "exercises/primitive_types/primitive_types1.rs"
mode = "compile" mode = "compile"
hint = "No hints this time ;)" hint = "这次没有提示 ;)"
[[exercises]] [[exercises]]
name = "primitive_types2" name = "primitive_types2"
path = "exercises/primitive_types/primitive_types2.rs" path = "exercises/primitive_types/primitive_types2.rs"
mode = "compile" mode = "compile"
hint = "No hints this time ;)" hint = "这次没有提示 ;)"
[[exercises]] [[exercises]]
name = "primitive_types3" name = "primitive_types3"
path = "exercises/primitive_types/primitive_types3.rs" path = "exercises/primitive_types/primitive_types3.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
There's a shorthand to initialize Arrays with a certain size that does not 便 100
require you to type in 100 items (but you certainly can if you want!).
For example, you can do:
let array = ["Are we there yet?"; 10]; let array = ["Are we there yet?"; 10];
Bonus: what are some other things you could have that would return true : 西 `a.len()>=100` true """
for `a.len() >= 100`?"""
[[exercises]] [[exercises]]
name = "primitive_types4" name = "primitive_types4"
path = "exercises/primitive_types/primitive_types4.rs" path = "exercises/primitive_types/primitive_types4.rs"
mode = "test" mode = "test"
hint = """ hint = """
Take a look at the Understanding Ownership -> Slices -> Other Slices section of the book: Understanding Ownership -> Slices -> Other Slices
https://doc.rust-lang.org/book/ch04-03-slices.html https://doc.rust-lang.org/book/ch04-03-slices.html
and use the starting and ending indices of the items in the Array
that you want to end up in the slice.
If you're curious why the first argument of `assert_eq!` does not `assert_eq!`
have an ampersand for a reference since the second argument is a 使 & Deref
reference, take a look at the Deref coercions section of the book:
https://doc.rust-lang.org/book/ch15-02-deref.html""" https://doc.rust-lang.org/book/ch15-02-deref.html"""
[[exercises]] [[exercises]]
@ -253,22 +250,20 @@ name = "primitive_types5"
path = "exercises/primitive_types/primitive_types5.rs" path = "exercises/primitive_types/primitive_types5.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Take a look at the Data Types -> The Tuple Type section of the book: Data Types -> The Tuple Type
https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type
Particularly the part about destructuring (second to last example in the section).
You'll need to make a pattern to bind `name` and `age` to the appropriate parts `name` `age` """
of the tuple. You can do it!!"""
[[exercises]] [[exercises]]
name = "primitive_types6" name = "primitive_types6"
path = "exercises/primitive_types/primitive_types6.rs" path = "exercises/primitive_types/primitive_types6.rs"
mode = "test" mode = "test"
hint = """ hint = """
While you could use a destructuring `let` for the tuple here, try 使 `let`
indexing into it instead, as explained in the last example of the Data Types -> The Tuple Type
Data Types -> The Tuple Type section of the book:
https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type
Now you have another tool in your toolbox!""" """
# STRUCTS # STRUCTS

Loading…
Cancel
Save