add zh/slice.md

This commit is contained in:
sunface 2022-02-27 21:28:49 +08:00
parent 984f8a8b12
commit b9637a6379
1 changed files with 17 additions and 15 deletions

View File

@ -1,10 +1,10 @@
# 切片
Slices are similar to arrays, but their length is not known at compile time, so you can't use slice directly.
# 切片( Slice )
切片跟数组相似,但是切片的长度无法在编译期得知,因此你无法直接使用切片类型。
🌟🌟 Here, both `[i32]` and `str` are slice types, but directly using it will cause errors. You have to use the reference of the slice instead: `&[i32]`, `&str`.
🌟🌟 这里, `[i32]``str` 都是切片类型,但是直接使用它们会造成编译错误,如下代码所示。为了解决,你需要使用切片的引用: `&[i32]`, `&str`.
```rust,editable
// fix the errors, DON'T add new lines!
// 修复代码中的错误,不要新增代码行!
fn main() {
let arr = [1, 2, 3];
let s1: [i32] = arr[0..2];
@ -13,7 +13,9 @@ fn main() {
}
```
A slice reference is a two-word object, for simplicity reasons, from now on we will use slice instead of `slice reference`. The first word is a pointer to the data, and the second word is the length of the slice. The word size is the same as usize, determined by the processor architecture eg 64 bits on an x86-64. Slices can be used to borrow a section of an array, and have the type signature `&[T]`.
一个切片引用占用了2个字大小的内存空间( 从现在开始,为了简洁性考虑,如无特殊原因,**我们统一使用切片来特指切片引用** )。 该切片的第一个字是指向数据的指针,第二个字是切片的长度。字的大小取决于处理器架构,例如在 `x86-64` 上,字的大小是 64 位也就是 8 个字节,那么一个切片引用就是 16 个字节大小。
切片( 引用 )可以用来借用数组的某个连续的部分,对应的签名是 `&[T]`,大家可以与数组的签名对比下 `[T; Length]`
🌟🌟🌟
```rust,editable
@ -23,8 +25,8 @@ fn main() {
let slice = &arr[..2];
// modify '6' to make it work
// TIPS: slice( reference ) IS NOT an array, if it is an array, then `assert!` will passed: each of the two UTF-8 chars '中' and '国' occupies 3 bytes, 2 * 3 = 6
// 修改数字 `6` 让代码工作
// 小提示: 切片和数组不一样,它是引用。如果是数组的话,那下面的 `assert!` 将会通过: 因为'中'和'国'是 UTF-8 字符,它们每个占用 3 个字节2 个字符就是 6 个字节
assert!(std::mem::size_of_val(&slice) == 6);
}
```
@ -34,13 +36,13 @@ fn main() {
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
// fill the blanks to make the code work
// 填空让代码工作起来
let slice: __ = __;
assert_eq!(slice, &[2, 3, 4]);
}
```
### string slices
### 字符串切片
🌟
```rust,editable
@ -48,7 +50,7 @@ fn main() {
let s = String::from("hello");
let slice1 = &s[0..2];
// fill the blank to make the code work
// 填空
let slice2 = &s[__];
assert_eq!(slice1, slice2);
@ -60,22 +62,22 @@ fn main() {
fn main() {
let s = "你好,世界";
// modify this line to make the code work
// 修改以下代码行,让代码工作起来
let slice = &s[0..2];
assert!(slice == "你");
}
```
🌟🌟 `&String` can be implicitly converted into `&str`.
🌟🌟 `&String` 可以被隐式地转换成 `&str` 类型.
```rust,editable
// fix errors
// 修复所有错误
fn main() {
let mut s = String::from("hello world");
// here, &s is `&String` type, but `first_word` need a `&str` type.
// it works because `&String` implicitly be converted to `&str, If you want know more ,this is called `Deref`
// 这里, &s 是 `&String` 类型,但是 `first_word` 函数需要的是 `&str` 类型。
// 尽管两个类型不一样,但是代码仍然可以工作,原因是 `&String` 会被隐式地转换成 `&str` 类型,如果大家想要知道更多,可以看看 Deref 章节: https://course.rs/advance/smart-pointer/deref.html
let word = first_word(&s);
s.clear(); // error!