mirror of https://github.com/sunface/rust-course
parent
973e0f7cac
commit
6f8f7bdff3
@ -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"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue