mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-25 00:18:46 +08:00
Clarify this code sample with less shadowing and more comments
This commit is contained in:
parent
9152b70a09
commit
8e5bba01d6
@ -288,13 +288,19 @@ with no loss of functionality:
|
|||||||
# &s[..]
|
# &s[..]
|
||||||
# }
|
# }
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("hello world");
|
let my_string = String::from("hello world");
|
||||||
let word = first_word(&s[..]);
|
|
||||||
|
|
||||||
let s = "hello world";
|
// first_word works on slices of `String`s
|
||||||
let word = first_word(&s[..]);
|
let word = first_word(&my_string[..]);
|
||||||
|
|
||||||
let word = first_word(s); // since literals are &strs, this works too!
|
let my_string_literal = "hello world";
|
||||||
|
|
||||||
|
// first_word works on slices of string literals
|
||||||
|
let word = first_word(&my_string_literal[..]);
|
||||||
|
|
||||||
|
// since string literals *are* string slices already,
|
||||||
|
// this works too, without the slice syntax!
|
||||||
|
let word = first_word(my_string_literal);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user