Skip to content

Commit 14c0b3a

Browse files
committed
auto merge of #15301 : jasonthompson/rust/docs/str2, r=alexcrichton
2 parents 3deb2c1 + 7e9bb8b commit 14c0b3a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Diff for: src/libcollections/str.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ Section: Creating a string
9797
///
9898
/// Returns `Err` with the original vector if the vector contains invalid
9999
/// UTF-8.
100+
///
101+
/// # Example
102+
///
103+
/// ```rust
104+
/// use std::str;
105+
/// let hello_vec = vec![104, 101, 108, 108, 111];
106+
/// let string = str::from_utf8_owned(hello_vec);
107+
/// assert_eq!(string, Ok("hello".to_string()));
108+
/// ```
100109
pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
101110
String::from_utf8(vv)
102111
}
@@ -111,22 +120,39 @@ pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
111120
///
112121
/// ```rust
113122
/// use std::str;
114-
/// let string = str::from_byte(66u8);
115-
/// assert_eq!(string.as_slice(), "B");
123+
/// let string = str::from_byte(104);
124+
/// assert_eq!(string.as_slice(), "h");
116125
/// ```
117126
pub fn from_byte(b: u8) -> String {
118127
assert!(b < 128u8);
119128
String::from_char(1, b as char)
120129
}
121130

122131
/// Convert a char to a string
132+
///
133+
/// # Example
134+
///
135+
/// ```rust
136+
/// use std::str;
137+
/// let string = str::from_char('b');
138+
/// assert_eq!(string.as_slice(), "b");
139+
/// ```
123140
pub fn from_char(ch: char) -> String {
124141
let mut buf = String::new();
125142
buf.push_char(ch);
126143
buf
127144
}
128145

129146
/// Convert a vector of chars to a string
147+
///
148+
/// # Example
149+
///
150+
/// ```rust
151+
/// use std::str;
152+
/// let chars = ['h', 'e', 'l', 'l', 'o'];
153+
/// let string = str::from_chars(chars);
154+
/// assert_eq!(string.as_slice(), "hello");
155+
/// ```
130156
pub fn from_chars(chs: &[char]) -> String {
131157
chs.iter().map(|c| *c).collect()
132158
}

0 commit comments

Comments
 (0)