Skip to content

Commit 84ef3b5

Browse files
author
Ulrik Sverdrup
committed
collections: Improve example for as_string and as_vec
1 parent f191f92 commit 84ef3b5

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
@@ -951,12 +951,13 @@ impl<'a> Deref for DerefString<'a> {
951951
/// # #![feature(collections)]
952952
/// use std::string::as_string;
953953
///
954-
/// fn string_consumer(s: String) {
955-
/// assert_eq!(s, "foo".to_string());
954+
/// // Let's pretend we have a function that requires `&String`
955+
/// fn string_consumer(s: &String) {
956+
/// assert_eq!(s, "foo");
956957
/// }
957958
///
958-
/// let string = as_string("foo").clone();
959-
/// string_consumer(string);
959+
/// // Provide a `&String` from a `&str` without allocating
960+
/// string_consumer(&as_string("foo"));
960961
/// ```
961962
#[unstable(feature = "collections")]
962963
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
@@ -1919,6 +1919,22 @@ impl<'a, T> Drop for DerefVec<'a, T> {
19191919
}
19201920

19211921
/// Converts a slice to a wrapper type providing a `&Vec<T>` reference.
1922+
///
1923+
/// # Examples
1924+
///
1925+
/// ```
1926+
/// # #![feature(collections)]
1927+
/// use std::vec::as_vec;
1928+
///
1929+
/// // Let's pretend we have a function that requires `&Vec<i32>`
1930+
/// fn vec_consumer(s: &Vec<i32>) {
1931+
/// assert_eq!(s, &[1, 2, 3]);
1932+
/// }
1933+
///
1934+
/// // Provide a `&Vec<i32>` from a `&[i32]` without allocating
1935+
/// let values = [1, 2, 3];
1936+
/// vec_consumer(&as_vec(&values));
1937+
/// ```
19221938
#[unstable(feature = "collections")]
19231939
pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
19241940
unsafe {

0 commit comments

Comments
 (0)