mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-04 01:42:54 +03:00
Add examples for StrVector methods
- examples for connect and concat - also fixed extra word in existing docs
This commit is contained in:
committed by
Alex Crichton
parent
2f355b79dd
commit
7db691e010
@@ -160,9 +160,27 @@ pub fn from_chars(chs: &[char]) -> String {
|
||||
/// Methods for vectors of strings
|
||||
pub trait StrVector {
|
||||
/// Concatenate a vector of strings.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let first = "Restaurant at the End of the".to_string();
|
||||
/// let second = " Universe".to_string();
|
||||
/// let string_vec = vec![first, second];
|
||||
/// assert_eq!(string_vec.concat(), "Restaurant at the End of the Universe".to_string());
|
||||
/// ```
|
||||
fn concat(&self) -> String;
|
||||
|
||||
/// Concatenate a vector of strings, placing a given separator between each.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let first = "Roast".to_string();
|
||||
/// let second = "Sirloin Steak".to_string();
|
||||
/// let string_vec = vec![first, second];
|
||||
/// assert_eq!(string_vec.connect(", "), "Roast, Sirloin Steak".to_string());
|
||||
/// ```
|
||||
fn connect(&self, sep: &str) -> String;
|
||||
}
|
||||
|
||||
@@ -172,7 +190,7 @@ fn concat(&self) -> String {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
// `len` calculation may overflow but push_str but will check boundaries
|
||||
// `len` calculation may overflow but push_str will check boundaries
|
||||
let len = self.iter().map(|s| s.as_slice().len()).sum();
|
||||
|
||||
let mut result = String::with_capacity(len);
|
||||
|
||||
Reference in New Issue
Block a user