Skip to content

Commit a75e308

Browse files
committed
rollup merge of #21780: steveklabnik/no_as_slice
Use auto deref instead.
2 parents 6ea5fb8 + 19a1f7e commit a75e308

File tree

5 files changed

+9
-29
lines changed

5 files changed

+9
-29
lines changed

src/doc/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ An example of each kind:
35183518
```{rust}
35193519
let vec: Vec<i32> = vec![1, 2, 3];
35203520
let arr: [i32; 3] = [1, 2, 3];
3521-
let s: &[i32] = vec.as_slice();
3521+
let s: &[i32] = &vec;
35223522
```
35233523

35243524
As you can see, the `vec!` macro allows you to create a `Vec<T>` easily. The

src/doc/trpl/more-strings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ To write a function that's generic over types of strings, use `&str`.
100100

101101
```
102102
fn some_string_length(x: &str) -> uint {
103-
x.len()
103+
x.len()
104104
}
105105
106106
fn main() {
@@ -110,7 +110,7 @@ fn main() {
110110
111111
let s = "Hello, world".to_string();
112112
113-
println!("{}", some_string_length(s.as_slice()));
113+
println!("{}", some_string_length(&s));
114114
}
115115
```
116116

src/doc/trpl/patterns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ match origin {
174174
}
175175
```
176176

177-
If you want to match against a slice or array, you can use `[]`:
177+
If you want to match against a slice or array, you can use `&`:
178178

179179
```{rust}
180180
fn main() {
181181
let v = vec!["match_this", "1"];
182182
183-
match v.as_slice() {
183+
match &v {
184184
["match_this", second] => println!("The second element is {}", second),
185185
_ => {},
186186
}

src/doc/trpl/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
8282
}
8383
};
8484
85-
let mut text = text.as_slice();
85+
let mut text = &text;
8686
let mut total = 0;
8787
while !text.is_empty() {
8888
match NUMERALS.iter().find(|&&(rn, _)| text.starts_with(rn)) {

src/doc/trpl/strings.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,16 @@ s.push_str(", world.");
3636
println!("{}", s);
3737
```
3838

39-
You can get a `&str` view into a `String` with the `as_slice()` method:
39+
`String`s will coerece into `&str` with an `&`:
4040

41-
```{rust}
41+
```
4242
fn takes_slice(slice: &str) {
4343
println!("Got: {}", slice);
4444
}
4545
4646
fn main() {
4747
let s = "Hello".to_string();
48-
takes_slice(s.as_slice());
49-
}
50-
```
51-
52-
To compare a String to a constant string, prefer `as_slice()`...
53-
54-
```{rust}
55-
fn compare(string: String) {
56-
if string.as_slice() == "Hello" {
57-
println!("yes");
58-
}
59-
}
60-
```
61-
62-
... over `to_string()`:
63-
64-
```{rust}
65-
fn compare(string: String) {
66-
if string == "Hello".to_string() {
67-
println!("yes");
68-
}
48+
takes_slice(&s);
6949
}
7050
```
7151

0 commit comments

Comments
 (0)