@@ -97,6 +97,15 @@ Section: Creating a string
97
97
///
98
98
/// Returns `Err` with the original vector if the vector contains invalid
99
99
/// 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
+ /// ```
100
109
pub fn from_utf8_owned ( vv : Vec < u8 > ) -> Result < String , Vec < u8 > > {
101
110
String :: from_utf8 ( vv)
102
111
}
@@ -111,22 +120,39 @@ pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
111
120
///
112
121
/// ```rust
113
122
/// 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 ");
116
125
/// ```
117
126
pub fn from_byte ( b : u8 ) -> String {
118
127
assert ! ( b < 128u8 ) ;
119
128
String :: from_char ( 1 , b as char )
120
129
}
121
130
122
131
/// 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
+ /// ```
123
140
pub fn from_char ( ch : char ) -> String {
124
141
let mut buf = String :: new ( ) ;
125
142
buf. push_char ( ch) ;
126
143
buf
127
144
}
128
145
129
146
/// 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
+ /// ```
130
156
pub fn from_chars ( chs : & [ char ] ) -> String {
131
157
chs. iter ( ) . map ( |c| * c) . collect ( )
132
158
}
0 commit comments