Skip to content

Commit 7c98626

Browse files
Ulrik Sverdrupsteveklabnik
authored andcommitted
collections: Improve example for as_string and as_vec
1 parent db1dc5d commit 7c98626

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/libcollections/string.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,13 @@ impl<'a> Deref for DerefString<'a> {
950950
/// # #![feature(collections)]
951951
/// use std::string::as_string;
952952
///
953-
/// fn string_consumer(s: String) {
954-
/// assert_eq!(s, "foo".to_string());
953+
/// // Let's pretend we have a function that requires `&String`
954+
/// fn string_consumer(s: &String) {
955+
/// assert_eq!(s, "foo");
955956
/// }
956957
///
957-
/// let string = as_string("foo").clone();
958-
/// string_consumer(string);
958+
/// // Provide a `&String` from a `&str` without allocating
959+
/// string_consumer(&as_string("foo"));
959960
/// ```
960961
#[unstable(feature = "collections")]
961962
pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {

src/libcollections/vec.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,22 @@ impl<'a, T> Drop for DerefVec<'a, T> {
19031903
}
19041904

19051905
/// Converts a slice to a wrapper type providing a `&Vec<T>` reference.
1906+
///
1907+
/// # Examples
1908+
///
1909+
/// ```
1910+
/// # #![feature(collections)]
1911+
/// use std::vec::as_vec;
1912+
///
1913+
/// // Let's pretend we have a function that requires `&Vec<i32>`
1914+
/// fn vec_consumer(s: &Vec<i32>) {
1915+
/// assert_eq!(s, &[1, 2, 3]);
1916+
/// }
1917+
///
1918+
/// // Provide a `&Vec<i32>` from a `&[i32]` without allocating
1919+
/// let values = [1, 2, 3];
1920+
/// vec_consumer(&as_vec(&values));
1921+
/// ```
19061922
#[unstable(feature = "collections")]
19071923
pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
19081924
unsafe {

0 commit comments

Comments
 (0)