Skip to content

Don't use as_slice() in docs #21780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3518,7 +3518,7 @@ An example of each kind:
```{rust}
let vec: Vec<i32> = vec![1, 2, 3];
let arr: [i32; 3] = [1, 2, 3];
let s: &[i32] = vec.as_slice();
let s: &[i32] = &vec;
```

As you can see, the `vec!` macro allows you to create a `Vec<T>` easily. The
Expand Down
4 changes: 2 additions & 2 deletions src/doc/trpl/more-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ To write a function that's generic over types of strings, use `&str`.

```
fn some_string_length(x: &str) -> uint {
x.len()
x.len()
}

fn main() {
Expand All @@ -110,7 +110,7 @@ fn main() {

let s = "Hello, world".to_string();

println!("{}", some_string_length(s.as_slice()));
println!("{}", some_string_length(&s));
}
```

Expand Down
4 changes: 2 additions & 2 deletions src/doc/trpl/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ match origin {
}
```

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

```{rust}
fn main() {
let v = vec!["match_this", "1"];

match v.as_slice() {
match &v {
["match_this", second] => println!("The second element is {}", second),
_ => {},
}
Expand Down
2 changes: 1 addition & 1 deletion src/doc/trpl/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
}
};

let mut text = text.as_slice();
let mut text = &text;
let mut total = 0;
while !text.is_empty() {
match NUMERALS.iter().find(|&&(rn, _)| text.starts_with(rn)) {
Expand Down
26 changes: 3 additions & 23 deletions src/doc/trpl/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,16 @@ s.push_str(", world.");
println!("{}", s);
```

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

```{rust}
```
fn takes_slice(slice: &str) {
println!("Got: {}", slice);
}

fn main() {
let s = "Hello".to_string();
takes_slice(s.as_slice());
}
```

To compare a String to a constant string, prefer `as_slice()`...

```{rust}
fn compare(string: String) {
if string.as_slice() == "Hello" {
println!("yes");
}
}
```

... over `to_string()`:

```{rust}
fn compare(string: String) {
if string == "Hello".to_string() {
println!("yes");
}
takes_slice(&s);
}
```

Expand Down