Merge pull request #207 from mg-chao/20220113_translate_strings

[rust-exercise] 翻译 strings 部分
pull/211/head
Sunface 3 years ago committed by GitHub
commit 0f407c9e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,7 @@
# Strings # 字符串
Rust has two string types, a string slice (`&str`) and an owned string (`String`). Rust 有两种字符串类型,一种是字符串切片(`&str`),另一种是拥有所有权的字符串(`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.
## Further information ## 更多信息
- [Strings](https://doc.rust-lang.org/book/ch08-02-strings.html) - [Strings](https://doc.rust-lang.org/book/ch08-02-strings.html)

@ -1,14 +1,15 @@
// strings1.rs // strings1.rs
// Make me compile without changing the function signature! // 在不改变函数签名的要求下通过编译!
// Execute `rustlings hint strings1` for hints ;) // 执行 `rustlings hint strings1` 获取提示 ;)
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let answer = current_favorite_color(); let answer = current_favorite_color();
println!("My current favorite color is {}", answer); println!("My current favorite color is {}", answer);// 译:"当前我最喜爱的颜色是 {}"
} }
// 译:当前最喜爱的颜色
fn current_favorite_color() -> String { fn current_favorite_color() -> String {
"blue" "blue"
} }

@ -1,18 +1,19 @@
// strings2.rs // strings2.rs
// Make me compile without changing the function signature! // 在不改变函数签名的要求下通过编译!
// Execute `rustlings hint strings2` for hints :) // 执行 `rustlings hint strings2` 获取提示 ;)
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let word = String::from("green"); // Try not changing this line :) let word = String::from("green"); // 尝试不更改这一行 :)
if is_a_color_word(word) { if is_a_color_word(word) {
println!("That is a color word I know!"); println!("That is a color word I know!");// 译:我知道这个颜色词
} else { } 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 { fn is_a_color_word(attempt: &str) -> bool {
attempt == "green" || attempt == "blue" || attempt == "red" attempt == "green" || attempt == "blue" || attempt == "red"
} }

@ -404,21 +404,19 @@ name = "strings1"
path = "exercises/strings/strings1.rs" path = "exercises/strings/strings1.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
The `current_favorite_color` function is currently returning a string slice with the `'static` `current_favorite_color` `'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` Strings
string slice covered in the Strings chapter of the book, and another way that uses the `From` 使 `From` """
trait."""
[[exercises]] [[exercises]]
name = "strings2" name = "strings2"
path = "exercises/strings/strings2.rs" path = "exercises/strings/strings2.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Yes, it would be really easy to fix this by just changing the value bound to `word` to be a `word` `String`
string slice instead of a `String`, wouldn't it?? There is a way to add one character to line 9 `String` """
9, though, that will coerce the `String` into a string slice."""
# TEST 2 # TEST 2

Loading…
Cancel
Save