From 19a1f7ed06d3f4f3b64cb4417f7159cd59f38ae7 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 30 Jan 2015 11:17:26 -0500 Subject: [PATCH] Don't use as_slice() in docs Use deref coercions instead. --- src/doc/reference.md | 2 +- src/doc/trpl/more-strings.md | 4 ++-- src/doc/trpl/patterns.md | 4 ++-- src/doc/trpl/plugins.md | 2 +- src/doc/trpl/strings.md | 26 +++----------------------- 5 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/doc/reference.md b/src/doc/reference.md index 59ac173f97a..86b7af9477e 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -3518,7 +3518,7 @@ An example of each kind: ```{rust} let vec: Vec = 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` easily. The diff --git a/src/doc/trpl/more-strings.md b/src/doc/trpl/more-strings.md index b4669b0819f..986ad23c665 100644 --- a/src/doc/trpl/more-strings.md +++ b/src/doc/trpl/more-strings.md @@ -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() { @@ -110,7 +110,7 @@ fn main() { let s = "Hello, world".to_string(); - println!("{}", some_string_length(s.as_slice())); + println!("{}", some_string_length(&s)); } ``` diff --git a/src/doc/trpl/patterns.md b/src/doc/trpl/patterns.md index 5c7b406a6fc..8240689c24b 100644 --- a/src/doc/trpl/patterns.md +++ b/src/doc/trpl/patterns.md @@ -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), _ => {}, } diff --git a/src/doc/trpl/plugins.md b/src/doc/trpl/plugins.md index 6e8e2c7ffe2..5ff233b4844 100644 --- a/src/doc/trpl/plugins.md +++ b/src/doc/trpl/plugins.md @@ -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)) { diff --git a/src/doc/trpl/strings.md b/src/doc/trpl/strings.md index 51f9356bd2f..e05c6e172a4 100644 --- a/src/doc/trpl/strings.md +++ b/src/doc/trpl/strings.md @@ -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); } ```