From 6f8f7bdff3e1221cecca662d04a43463403bc9f3 Mon Sep 17 00:00:00 2001 From: mg-chao Date: Fri, 14 Jan 2022 20:20:25 +0800 Subject: [PATCH] update: translate strings --- exercise/exercises/strings/README.md | 8 +++----- exercise/exercises/strings/strings1.rs | 7 ++++--- exercise/exercises/strings/strings2.rs | 11 ++++++----- exercise/info.toml | 16 +++++++--------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/exercise/exercises/strings/README.md b/exercise/exercises/strings/README.md index fa2104cc..9776444c 100644 --- a/exercise/exercises/strings/README.md +++ b/exercise/exercises/strings/README.md @@ -1,9 +1,7 @@ -# Strings +# 字符串 -Rust has two string types, a string slice (`&str`) and an owned string (`String`). -We're not going to dictate when you should use which one, but we'll show you how -to identify and create them, as well as use them. +Rust 有两种字符串类型,一种是字符串切片(`&str`),另一种是拥有所有权的字符串(`String`)。我们不打算向你说明何时使用其中哪一种,但我们将为你讲解如何区分和创建它们,并灵活使用。 -## Further information +## 更多信息 - [Strings](https://doc.rust-lang.org/book/ch08-02-strings.html) diff --git a/exercise/exercises/strings/strings1.rs b/exercise/exercises/strings/strings1.rs index 80902444..16549ef8 100644 --- a/exercise/exercises/strings/strings1.rs +++ b/exercise/exercises/strings/strings1.rs @@ -1,14 +1,15 @@ // strings1.rs -// Make me compile without changing the function signature! -// Execute `rustlings hint strings1` for hints ;) +// 在不改变函数签名的要求下通过编译! +// 执行 `rustlings hint strings1` 获取提示 ;) // I AM NOT DONE fn main() { let answer = current_favorite_color(); - println!("My current favorite color is {}", answer); + println!("My current favorite color is {}", answer);// 译:"当前我最喜爱的颜色是 {}" } +// 译:当前最喜爱的颜色 fn current_favorite_color() -> String { "blue" } diff --git a/exercise/exercises/strings/strings2.rs b/exercise/exercises/strings/strings2.rs index 5a2ce74a..9b1e4011 100644 --- a/exercise/exercises/strings/strings2.rs +++ b/exercise/exercises/strings/strings2.rs @@ -1,18 +1,19 @@ // strings2.rs -// Make me compile without changing the function signature! -// Execute `rustlings hint strings2` for hints :) +// 在不改变函数签名的要求下通过编译! +// 执行 `rustlings hint strings2` 获取提示 ;) // I AM NOT DONE fn main() { - let word = String::from("green"); // Try not changing this line :) + let word = String::from("green"); // 尝试不更改这一行 :) if is_a_color_word(word) { - println!("That is a color word I know!"); + println!("That is a color word I know!");// 译:我知道这个颜色词 } else { - println!("That is not a color word I know."); + println!("That is not a color word I know.");// 译:我不知道这个颜色词 } } +// 译:是否是颜色词 fn is_a_color_word(attempt: &str) -> bool { attempt == "green" || attempt == "blue" || attempt == "red" } diff --git a/exercise/info.toml b/exercise/info.toml index 2804ab00..05d3bc33 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -404,21 +404,19 @@ name = "strings1" path = "exercises/strings/strings1.rs" mode = "compile" hint = """ -The `current_favorite_color` function is currently returning a string slice with the `'static` -lifetime. We know this because the data of the string lives in our code itself -- it doesn't -come from a file or user input or another program -- so it will live as long as our program -lives. But it is still a string slice. There's one way to create a `String` by converting a -string slice covered in the Strings chapter of the book, and another way that uses the `From` -trait.""" +`current_favorite_color` 函数返回的是一个生命周期为 `'static` 的字符串切片。 +我们知道这点是因为字符串直接存储在了代码中——它并不来自于文件、用户输入或 +其他程序——所以只要程序还在运行,它就会一直存在。但它仍然是一个字符串切片。 +有一种方法可以将字符串切片转换为 `String`,这在书的 Strings 章节有所介绍,还有一种 +方法是使用 `From` 特征""" [[exercises]] name = "strings2" path = "exercises/strings/strings2.rs" mode = "compile" hint = """ -Yes, it would be really easy to fix this by just changing the value bound to `word` to be a -string slice instead of a `String`, wouldn't it?? There is a way to add one character to line -9, though, that will coerce the `String` into a string slice.""" +是的,只要把绑定在 `word` 上的值改为字符串切片而非 `String` 就可以很容易地解决这个问题,不是吗?? +有个方法是在第 9 行添一个字符,这将强制把 `String` 转为字符串切片。""" # TEST 2