diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-04-27 10:26:18 -0400 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-04-27 10:26:18 -0400 |
| commit | 72ee472146681d2a87e9bdce8024da309828dafd (patch) | |
| tree | 670a5c85c63f5684ad1d14f34be8ead0ab5a4098 | |
| parent | 130240afc6eb1da5131f4a66d9e374a28eb6102c (diff) | |
| parent | 84ef3b53517e2ed1148e6d4214c0c97c2328ead0 (diff) | |
| download | rust-72ee472146681d2a87e9bdce8024da309828dafd.tar.gz rust-72ee472146681d2a87e9bdce8024da309828dafd.zip | |
Rollup merge of #24848 - bluss:deref-string, r=alexcrichton
Improve example for as_string and add example for as_vec Provide a better example of `as_string` / `DerefString`'s unique capabilities. Use an example where (for an unspecified reason) you need a &String, and show how `as_string` solves the problem without needing an allocation.
| -rw-r--r-- | src/libcollections/string.rs | 9 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 16 |
2 files changed, 21 insertions, 4 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index a37a26ef22a..be6405dc85a 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -951,12 +951,13 @@ impl<'a> Deref for DerefString<'a> { /// # #![feature(collections)] /// use std::string::as_string; /// -/// fn string_consumer(s: String) { -/// assert_eq!(s, "foo".to_string()); +/// // Let's pretend we have a function that requires `&String` +/// fn string_consumer(s: &String) { +/// assert_eq!(s, "foo"); /// } /// -/// let string = as_string("foo").clone(); -/// string_consumer(string); +/// // Provide a `&String` from a `&str` without allocating +/// string_consumer(&as_string("foo")); /// ``` #[unstable(feature = "collections")] pub fn as_string<'a>(x: &'a str) -> DerefString<'a> { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index c8a8498d2f9..fcf90a84ef2 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1915,6 +1915,22 @@ impl<'a, T> Drop for DerefVec<'a, T> { } /// Converts a slice to a wrapper type providing a `&Vec<T>` reference. +/// +/// # Examples +/// +/// ``` +/// # #![feature(collections)] +/// use std::vec::as_vec; +/// +/// // Let's pretend we have a function that requires `&Vec<i32>` +/// fn vec_consumer(s: &Vec<i32>) { +/// assert_eq!(s, &[1, 2, 3]); +/// } +/// +/// // Provide a `&Vec<i32>` from a `&[i32]` without allocating +/// let values = [1, 2, 3]; +/// vec_consumer(&as_vec(&values)); +/// ``` #[unstable(feature = "collections")] pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> { unsafe { |
