Skip to content

Implement resize for Vec #20044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,33 @@ impl<T: Clone> Vec<T> {
}
}

/// Resizes the `Vec` in-place so that `len()` is equal to `new_len`.
///
/// Calls either `extend()` or `truncate()` depending on whether `new_len`
/// is larger than the current value of `len()` or not.
///
/// # Examples
///
/// ```
/// let mut vec = vec!["hello"];
/// vec.resize(3, "world");
/// assert_eq!(vec, vec!["hello", "world", "world"]);
///
/// let mut vec = vec![1i, 2, 3, 4];
/// vec.resize(2, 0);
/// assert_eq!(vec, vec![1, 2]);
/// ```
#[unstable = "matches collection reform specification; waiting for dust to settle"]
pub fn resize(&mut self, new_len: uint, value: T) {
let len = self.len();

if new_len > len {
self.extend(repeat(value).take(new_len - len));
} else {
self.truncate(new_len);
}
}

/// Partitions a vector based on a predicate.
///
/// Clones the elements of the vector, partitioning them into two `Vec<T>`s
Expand Down