diff --git a/src/collections/string.md b/src/collections/string.md index f65c736..54caecd 100644 --- a/src/collections/string.md +++ b/src/collections/string.md @@ -34,7 +34,6 @@ A `String` is stored as a vector of bytes (`Vec`), but guaranteed to always ```rust,editable // FILL in the blanks fn main() { - // get a slice of String with reference: String -> &str let mut s = String::from("hello, world"); let slice1: &str = __; // in two ways diff --git a/zh-CN/src/collections/String.md b/zh-CN/src/collections/String.md index f65c736..123546d 100644 --- a/zh-CN/src/collections/String.md +++ b/zh-CN/src/collections/String.md @@ -1,13 +1,13 @@ # String -`std::string::String` is a UTF-8 encoded, growable string. It is the most common string type we used in daily dev, it also has ownership over the string contents. +`std::string::String` 是 UTF-8 编码、可增长的动态字符串. 它也是我们日常开发中最常用的字符串类型,同时对于它所拥有的内容拥有所有权。 -### Basic operations +### 基本操作 1. 🌟🌟 ```rust,editable -// FILL in the blanks and FIX errors -// 1. Don't use `to_string()` -// 2. Dont't add/remove any code line +// 填空并修复错误 +// 1. 不要使用 `to_string()` +// 2. 不要添加/删除任何代码行 fn main() { let mut s: String = "hello, "; s.push_str("world".to_string()); @@ -26,18 +26,20 @@ fn move_ownership(s: String) { ``` ### String and &str -A `String` is stored as a vector of bytes (`Vec`), but guaranteed to always be a valid UTF-8 sequence. `String` is heap allocated, growable and not null terminated. +虽然 `String` 的底层是 `Vec` 也就是字节数组的形式存储的,但是它是基于 UTF-8 编码的字符序列。`String` 分配在堆上、可增长且不是以 `null` 结尾。 + +而 `&str` 是[切片引用](https://course.rs/confonding/slice.html)类型( `&[u8]` ),指向一个合法的 UTF-8 字符序列,总之,`&str` 和 `String` 的关系类似于 `&[T]` 和 `Vec` 。 + +如果大家想了解更多,可以看看[易混淆概念解析 - &str 和 String](https://course.rs/confonding/string.html)。 -`&str` is a slice (`&[u8]`) that always points to a valid UTF-8 sequence, and can be used to view into a String, just like `&[T]` is a view into `Vec`. 2. 🌟🌟 ```rust,editable -// FILL in the blanks +// 填空 fn main() { - // get a slice of String with reference: String -> &str let mut s = String::from("hello, world"); - let slice1: &str = __; // in two ways + let slice1: &str = __; // 使用两种方法 assert_eq!(slice1, "hello, world"); let slice2 = __; diff --git a/zh-CN/src/generics-traits/advanced-traits.md b/zh-CN/src/generics-traits/advanced-traits.md index 6cebfb5..4533f3f 100644 --- a/zh-CN/src/generics-traits/advanced-traits.md +++ b/zh-CN/src/generics-traits/advanced-traits.md @@ -61,7 +61,7 @@ fn main() { ## 定义默认的泛型类型参数 当我们使用泛型类型参数时,可以为该泛型参数指定一个具体的默认类型,这样当实现该特征时,如果该默认类型可以使用,那用户再无需手动指定具体的类型。 -1. 🌟🌟 +2. 🌟🌟 ```rust,editable use std::ops::Sub; @@ -188,7 +188,7 @@ fn main() { ## Supertraits 有些时候我们希望在特征上实现类似继承的特性,例如让一个特征 `A` 使用另一个特征 `B` 的功能。这种情况下,一个类型要实现特征 `A` 首先要实现特征 `B`, 特征 `B` 就被称为 `supertrait` -1. 🌟🌟🌟 +4. 🌟🌟🌟 ```rust,editable trait Person { @@ -248,7 +248,7 @@ fn main() { 关于孤儿原则的详细介绍请参见[特征定义与实现的位置孤儿规则](https://course.rs/basic/trait/trait#特征定义与实现的位置孤儿规则) 和 [在外部类型上实现外部特征](https://course.rs/basic/trait/advance-trait.html#在外部类型上实现外部特征newtype)。 -1. 🌟🌟 +5. 🌟🌟 ```rust use std::fmt;