#![stable(feature = "rust1", since = "1.0.0")]
-use alloc::raw_vec::RawVec;
use alloc::boxed::Box;
use alloc::heap::EMPTY;
+use alloc::raw_vec::RawVec;
+use borrow::ToOwned;
+use borrow::Cow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
-use core::intrinsics::{arith_offset, assume, needs_drop};
+use core::intrinsics::{arith_offset, assume};
use core::iter::FromIterator;
use core::mem;
use core::ops::{Index, IndexMut};
use core::ptr;
use core::slice;
-#[allow(deprecated)]
-use borrow::{Cow, IntoCow};
-
use super::range::RangeArgument;
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector.'
/// }
/// ```
///
+/// # Indexing
+///
+/// The Vec type allows to access values by index, because it implements the
+/// `Index` trait. An example will be more explicit:
+///
+/// ```
+/// let v = vec!(0, 2, 4, 6);
+/// println!("{}", v[1]); // it will display '2'
+/// ```
+///
+/// However be careful: if you try to access an index which isn't in the Vec,
+/// your software will panic! You cannot do this:
+///
+/// ```ignore
+/// let v = vec!(0, 2, 4, 6);
+/// println!("{}", v[6]); // it will panic!
+/// ```
+///
+/// In conclusion: always check if the index you want to get really exists
+/// before doing it.
+///
+/// # Slicing
+///
+/// A Vec can be mutable. Slices, on the other hand, are read-only objects.
+/// To get a slice, use "&". Example:
+///
+/// ```
+/// fn read_slice(slice: &[usize]) {
+/// // ...
+/// }
+///
+/// let v = vec!(0, 1);
+/// read_slice(&v);
+///
+/// // ... and that's all!
+/// // you can also do it like this:
+/// let x : &[usize] = &v;
+/// ```
+///
+/// In Rust, it's more common to pass slices as arguments rather than vectors
+/// when you just want to provide a read access. The same goes for String and
+/// &str.
+///
/// # Capacity and reallocation
///
/// The capacity of a vector is the amount of space allocated for any future
unsafe {
// drop any extra elements
while len < self.len {
- // decrement len before the read(), so a panic on Drop doesn't
- // re-drop the just-failed value.
+ // decrement len before the drop_in_place(), so a panic on Drop
+ // doesn't re-drop the just-failed value.
self.len -= 1;
- ptr::read(self.get_unchecked(self.len));
+ let len = self.len;
+ ptr::drop_in_place(self.get_unchecked_mut(len));
}
}
}
}
}
- #[allow(missing_docs)]
- #[inline]
- #[unstable(feature = "vec_push_all",
- reason = "likely to be replaced by a more optimized extend",
- issue = "27744")]
- #[rustc_deprecated(reason = "renamed to extend_from_slice",
- since = "1.6.0")]
- pub fn push_all(&mut self, other: &[T]) {
- self.extend_from_slice(other)
- }
-
/// Appends all elements in a slice to the `Vec`.
///
/// Iterates over the slice `other`, clones each element, and then appends
self
}
}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::Index<ops::RangeInclusive<usize>> for Vec<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: ops::RangeInclusive<usize>) -> &[T] {
+ Index::index(&**self, index)
+ }
+}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::Index<ops::RangeToInclusive<usize>> for Vec<T> {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: ops::RangeToInclusive<usize>) -> &[T] {
+ Index::index(&**self, index)
+ }
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
self
}
}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for Vec<T> {
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut [T] {
+ IndexMut::index_mut(&mut **self, index)
+ }
+}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for Vec<T> {
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] {
+ IndexMut::index_mut(&mut **self, index)
+ }
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Deref for Vec<T> {
fn drop(&mut self) {
if self.buf.unsafe_no_drop_flag_needs_drop() {
unsafe {
- // The branch on needs_drop() is an -O1 performance optimization.
- // Without the branch, dropping Vec<u8> takes linear time.
- if needs_drop::<T>() {
- for x in self.iter_mut() {
- ptr::drop_in_place(x);
- }
- }
+ // use drop for [T]
+ ptr::drop_in_place(&mut self[..]);
}
}
// RawVec handles deallocation
}
}
-#[stable(feature = "rust1", since = "1.0.0")]
-#[allow(deprecated)]
-impl<'a, T: 'a> IntoCow<'a, [T]> for Vec<T> where T: Clone {
- fn into_cow(self) -> Cow<'a, [T]> {
- Cow::Owned(self)
- }
-}
-
-#[stable(feature = "rust1", since = "1.0.0")]
-#[allow(deprecated)]
-impl<'a, T> IntoCow<'a, [T]> for &'a [T] where T: Clone {
- fn into_cow(self) -> Cow<'a, [T]> {
- Cow::Borrowed(self)
- }
-}
-
////////////////////////////////////////////////////////////////////////////////
// Iterators
////////////////////////////////////////////////////////////////////////////////
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {}
+#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
+impl<T: Clone> Clone for IntoIter<T> {
+ fn clone(&self) -> IntoIter<T> {
+ unsafe {
+ slice::from_raw_parts(self.ptr, self.len()).to_owned().into_iter()
+ }
+ }
+}
+
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Drop for IntoIter<T> {
#[unsafe_destructor_blind_to_params]