Skip to content

Commit

Permalink
Add examples for from_utf8_owned, from_char, from_chars, from_byte
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonthompson committed Jul 1, 2014
1 parent 6ab7b66 commit 7e9bb8b
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/libcollections/str.rs
Expand Up @@ -97,6 +97,15 @@ Section: Creating a string
///
/// Returns `Err` with the original vector if the vector contains invalid
/// UTF-8.
///
/// # Example
///
/// ```rust
/// use std::str;
/// let hello_vec = vec![104, 101, 108, 108, 111];
/// let string = str::from_utf8_owned(hello_vec);
/// assert_eq!(string, Ok("hello".to_string()));
/// ```
pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
String::from_utf8(vv)
}
Expand All @@ -111,22 +120,39 @@ pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
///
/// ```rust
/// use std::str;
/// let string = str::from_byte(66u8);
/// assert_eq!(string.as_slice(), "B");
/// let string = str::from_byte(104);
/// assert_eq!(string.as_slice(), "h");
/// ```
pub fn from_byte(b: u8) -> String {
assert!(b < 128u8);
String::from_char(1, b as char)
}

/// Convert a char to a string
///
/// # Example
///
/// ```rust
/// use std::str;
/// let string = str::from_char('b');
/// assert_eq!(string.as_slice(), "b");
/// ```
pub fn from_char(ch: char) -> String {
let mut buf = String::new();
buf.push_char(ch);
buf
}

/// Convert a vector of chars to a string
///
/// # Example
///
/// ```rust
/// use std::str;
/// let chars = ['h', 'e', 'l', 'l', 'o'];
/// let string = str::from_chars(chars);
/// assert_eq!(string.as_slice(), "hello");
/// ```
pub fn from_chars(chs: &[char]) -> String {
chs.iter().map(|c| *c).collect()
}
Expand Down

5 comments on commit 7e9bb8b

@bors
Copy link
Contributor

@bors bors commented on 7e9bb8b Jul 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at jasonthompson@7e9bb8b

@bors
Copy link
Contributor

@bors bors commented on 7e9bb8b Jul 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jasonthompson/rust/docs/str2 = 7e9bb8b into auto

@bors
Copy link
Contributor

@bors bors commented on 7e9bb8b Jul 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jasonthompson/rust/docs/str2 = 7e9bb8b merged ok, testing candidate = 14c0b3a

@bors
Copy link
Contributor

@bors bors commented on 7e9bb8b Jul 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 14c0b3a

Please sign in to comment.