]> git.lizzy.rs Git - rust.git/commitdiff
collections: Improve example for as_string and as_vec
authorUlrik Sverdrup <root@localhost>
Sun, 26 Apr 2015 20:13:58 +0000 (22:13 +0200)
committerUlrik Sverdrup <root@localhost>
Mon, 27 Apr 2015 00:10:42 +0000 (02:10 +0200)
src/libcollections/string.rs
src/libcollections/vec.rs

index a37a26ef22ac3af2852ebeb9c106fa799a3445cc..be6405dc85a1498fcbffba5194c8397c1754ebd9 100644 (file)
@@ -951,12 +951,13 @@ fn deref<'b>(&'b self) -> &'b String {
 /// # #![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> {
index 526150915a705a0fb0289774776e5aae88c03b42..98819e0d7ce6fb5549765309c24743ac4a58b874 100644 (file)
@@ -1919,6 +1919,22 @@ fn drop(&mut self) {
 }
 
 /// 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 {