Lines Matching full:vec
4 //! `Vec<T>`.
13 //! You can explicitly create a [`Vec`] with [`Vec::new`]:
16 //! let v: Vec<i32> = Vec::new();
19 //! ...or by using the [`vec!`] macro:
22 //! let v: Vec<i32> = vec![];
24 //! let v = vec![1, 2, 3, 4, 5];
26 //! let v = vec![0; 10]; // ten zeroes
33 //! let mut v = vec![1, 2];
41 //! let mut v = vec![1, 2];
49 //! let mut v = vec![1, 2, 3];
54 //! [`push`]: Vec::push
150 /// A contiguous growable array type, written as `Vec<T>`, short for 'vector'.
155 /// let mut vec = Vec::new();
156 /// vec.push(1);
157 /// vec.push(2);
159 /// assert_eq!(vec.len(), 2);
160 /// assert_eq!(vec[0], 1);
162 /// assert_eq!(vec.pop(), Some(2));
163 /// assert_eq!(vec.len(), 1);
165 /// vec[0] = 7;
166 /// assert_eq!(vec[0], 7);
168 /// vec.extend([1, 2, 3]);
170 /// for x in &vec {
173 /// assert_eq!(vec, [7, 1, 2, 3]);
176 /// The [`vec!`] macro is provided for convenient initialization:
179 /// let mut vec1 = vec![1, 2, 3];
181 /// let vec2 = Vec::from([1, 2, 3, 4]);
185 /// It can also initialize each element of a `Vec<T>` with a given value.
190 /// let vec = vec![0; 5];
191 /// assert_eq!(vec, [0, 0, 0, 0, 0]);
194 /// let mut vec = Vec::with_capacity(5);
195 /// vec.resize(5, 0);
196 /// assert_eq!(vec, [0, 0, 0, 0, 0]);
202 /// Use a `Vec<T>` as an efficient stack:
205 /// let mut stack = Vec::new();
219 /// The `Vec` type allows access to values by index, because it implements the
223 /// let v = vec![0, 2, 4, 6];
227 /// However be careful: if you try to access an index which isn't in the `Vec`,
231 /// let v = vec![0, 2, 4, 6];
236 /// the `Vec`.
240 /// A `Vec` can be mutable. On the other hand, slices are read-only objects.
248 /// let v = vec![0, 1];
275 /// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`]
280 /// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees
283 /// by unsafe code. Note that these guarantees refer to an unqualified `Vec<T>`.
287 /// Most fundamentally, `Vec` is and always will be a (pointer, capacity, length)
293 /// if you construct a `Vec` with capacity 0 via [`Vec::new`], [`vec![]`][`vec!`],
294 /// [`Vec::with_capacity(0)`][`Vec::with_capacity`], or by calling [`shrink_to_fit`]
295 /// on an empty Vec, it will not allocate memory. Similarly, if you store zero-sized
296 /// types inside a `Vec`, it will not allocate space for them. *Note that in this case
297 /// the `Vec` might not report a [`capacity`] of 0*. `Vec` will allocate if and only
298 /// if <code>[mem::size_of::\<T>]\() * [capacity]\() > 0</code>. In general, `Vec`'s allocation
299 /// details are very subtle --- if you intend to allocate memory using a `Vec`
302 /// `from_raw_parts` to recover the `Vec` and then dropping it.
304 /// If a `Vec` *has* allocated memory, then the memory it points to is on the heap
311 /// visualized as below. The top part is the `Vec` struct, it contains a
328 /// - Note: the ABI is not stable and `Vec` makes no guarantees about its memory
331 /// `Vec` will never perform a "small optimization" where elements are actually
335 /// a `Vec`. The contents of a `Vec` wouldn't have a stable address if it were
336 /// only moved, and it would be more difficult to determine if a `Vec` had
342 /// `Vec` will never automatically shrink itself, even if completely empty. This
343 /// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
352 /// allocated by a `Vec` if desired. Bulk insertion methods *may* reallocate, even
355 /// `Vec` does not guarantee any particular growth strategy when reallocating
360 /// `vec![x; n]`, `vec![a, b, c, d]`, and
361 /// [`Vec::with_capacity(n)`][`Vec::with_capacity`], will all produce a `Vec`
363 /// (as is the case for the [`vec!`] macro), then a `Vec<T>` can be converted to
366 /// `Vec` will not specifically overwrite any data that is removed from it,
370 /// removed data to be erased for security purposes. Even if you drop a `Vec`, its
371 /// buffer may simply be reused by another allocation. Even if you zero a `Vec`'s memory
377 /// Currently, `Vec` does not guarantee the order in which elements are dropped.
384 /// [`shrink_to_fit`]: Vec::shrink_to_fit
385 /// [`shrink_to`]: Vec::shrink_to
386 /// [capacity]: Vec::capacity
387 /// [`capacity`]: Vec::capacity
389 /// [len]: Vec::len
390 /// [`len`]: Vec::len
391 /// [`push`]: Vec::push
392 /// [`insert`]: Vec::insert
393 /// [`reserve`]: Vec::reserve
397 #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")]
399 pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> { struct
408 impl<T> Vec<T> { implementation
409 /// Constructs a new, empty `Vec<T>`.
417 /// let mut vec: Vec<i32> = Vec::new();
424 Vec { buf: RawVec::NEW, len: 0 } in new()
427 /// Constructs a new, empty `Vec<T>` with at least the specified capacity.
438 /// If it is important to know the exact allocated capacity of a `Vec`,
441 /// For `Vec<T>` where `T` is a zero-sized type, there will be no allocation
445 /// [`capacity`]: Vec::capacity
454 /// let mut vec = Vec::with_capacity(10);
457 /// assert_eq!(vec.len(), 0);
458 /// assert!(vec.capacity() >= 10);
462 /// vec.push(i);
464 /// assert_eq!(vec.len(), 10);
465 /// assert!(vec.capacity() >= 10);
468 /// vec.push(11);
469 /// assert_eq!(vec.len(), 11);
470 /// assert!(vec.capacity() >= 11);
474 /// let vec_units = Vec::<()>::with_capacity(10);
485 /// Tries to construct a new, empty `Vec<T>` with at least the specified capacity.
496 /// If it is important to know the exact allocated capacity of a `Vec`,
499 /// For `Vec<T>` where `T` is a zero-sized type, there will be no allocation
503 /// [`capacity`]: Vec::capacity
508 /// let mut vec = Vec::try_with_capacity(10).unwrap();
511 /// assert_eq!(vec.len(), 0);
512 /// assert!(vec.capacity() >= 10);
516 /// vec.push(i);
518 /// assert_eq!(vec.len(), 10);
519 /// assert!(vec.capacity() >= 10);
522 /// vec.push(11);
523 /// assert_eq!(vec.len(), 11);
524 /// assert!(vec.capacity() >= 11);
526 /// let mut result = Vec::try_with_capacity(usize::MAX);
531 /// let vec_units = Vec::<()>::try_with_capacity(10).unwrap();
540 /// Creates a `Vec<T>` directly from a pointer, a capacity, and a length.
563 /// via `Vec<T>`. Other allocation sources are allowed if the invariants are
568 /// to build a `Vec<u8>` from a pointer to a C `char` array with length
570 /// a `Vec` or `String`.
571 /// It's also not safe to build one from a `Vec<u16>` and its length, because
574 /// turning it into a `Vec<u8>` it'll be deallocated with alignment 1. To avoid
579 /// `Vec<T>` which may then deallocate, reallocate or change the
594 /// let v = vec![1, 2, 3];
612 /// // Put everything back together into a Vec
613 /// let rebuilt = Vec::from_raw_parts(p, len, cap);
626 /// let vec = unsafe {
634 /// Vec::from_raw_parts(mem, 1, 16)
637 /// assert_eq!(vec, &[1_000_000]);
638 /// assert_eq!(vec.capacity(), 16);
648 impl<T, A: Allocator> Vec<T, A> { implementation
649 /// Constructs a new, empty `Vec<T, A>`.
661 /// let mut vec: Vec<i32, _> = Vec::new_in(System);
666 Vec { buf: RawVec::new_in(alloc), len: 0 } in new_in()
669 /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
681 /// If it is important to know the exact allocated capacity of a `Vec`,
684 /// For `Vec<T, A>` where `T` is a zero-sized type, there will be no allocation
688 /// [`capacity`]: Vec::capacity
701 /// let mut vec = Vec::with_capacity_in(10, System);
704 /// assert_eq!(vec.len(), 0);
705 /// assert!(vec.capacity() >= 10);
709 /// vec.push(i);
711 /// assert_eq!(vec.len(), 10);
712 /// assert!(vec.capacity() >= 10);
715 /// vec.push(11);
716 /// assert_eq!(vec.len(), 11);
717 /// assert!(vec.capacity() >= 11);
721 /// let vec_units = Vec::<(), System>::with_capacity_in(10, System);
728 Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } in with_capacity_in()
731 /// Tries to construct a new, empty `Vec<T, A>` with at least the specified capacity
743 /// If it is important to know the exact allocated capacity of a `Vec`,
746 /// For `Vec<T, A>` where `T` is a zero-sized type, there will be no allocation
750 /// [`capacity`]: Vec::capacity
759 /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
762 /// assert_eq!(vec.len(), 0);
763 /// assert!(vec.capacity() >= 10);
767 /// vec.push(i);
769 /// assert_eq!(vec.len(), 10);
770 /// assert!(vec.capacity() >= 10);
773 /// vec.push(11);
774 /// assert_eq!(vec.len(), 11);
775 /// assert!(vec.capacity() >= 11);
777 /// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
782 /// let vec_units = Vec::<(), System>::try_with_capacity_in(10, System).unwrap();
788 Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) in try_with_capacity_in()
791 /// Creates a `Vec<T, A>` directly from a pointer, a capacity, a length,
814 /// via `Vec<T, A>`. Other allocation sources are allowed if the invariants are
819 /// to build a `Vec<u8>` from a pointer to a C `char` array with length `size_t`.
820 /// It's also not safe to build one from a `Vec<u16>` and its length, because
823 /// turning it into a `Vec<u8>` it'll be deallocated with alignment 1.
826 /// `Vec<T>` which may then deallocate, reallocate or change the
846 /// let mut v = Vec::with_capacity_in(3, System);
868 /// // Put everything back together into a Vec
869 /// let rebuilt = Vec::from_raw_parts_in(p, len, cap, alloc.clone());
884 /// let vec = unsafe {
892 /// Vec::from_raw_parts_in(mem, 1, 16, Global)
895 /// assert_eq!(vec, &[1_000_000]);
896 /// assert_eq!(vec.capacity(), 16);
902 unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } } in from_raw_parts_in()
905 /// Decomposes a `Vec<T>` into its raw components.
913 /// memory previously managed by the `Vec`. The only way to do
915 /// into a `Vec` with the [`from_raw_parts`] function, allowing
918 /// [`from_raw_parts`]: Vec::from_raw_parts
924 /// let v: Vec<i32> = vec![-1, 0, 1];
933 /// Vec::from_raw_parts(ptr, len, cap)
943 /// Decomposes a `Vec<T>` into its raw components.
950 /// memory previously managed by the `Vec`. The only way to do
952 /// into a `Vec` with the [`from_raw_parts_in`] function, allowing
955 /// [`from_raw_parts_in`]: Vec::from_raw_parts_in
964 /// let mut v: Vec<i32, System> = Vec::new_in(System);
976 /// Vec::from_raw_parts_in(ptr, len, cap, alloc)
997 /// let mut vec: Vec<i32> = Vec::with_capacity(10);
998 /// vec.push(42);
999 /// assert!(vec.capacity() >= 10);
1008 /// in the given `Vec<T>`. The collection may reserve more space to
1020 /// let mut vec = vec![1];
1021 /// vec.reserve(10);
1022 /// assert!(vec.capacity() >= 11);
1031 /// be inserted in the given `Vec<T>`. Unlike [`reserve`], this will not
1041 /// [`reserve`]: Vec::reserve
1050 /// let mut vec = vec![1];
1051 /// vec.reserve_exact(10);
1052 /// assert!(vec.capacity() >= 11);
1061 /// in the given `Vec<T>`. The collection may reserve more space to speculatively avoid
1077 /// fn process_data(data: &[u32]) -> Result<Vec<u32>, TryReserveError> {
1078 /// let mut output = Vec::new();
1098 /// elements to be inserted in the given `Vec<T>`. Unlike [`try_reserve`],
1108 /// [`try_reserve`]: Vec::try_reserve
1120 /// fn process_data(data: &[u32]) -> Result<Vec<u32>, TryReserveError> {
1121 /// let mut output = Vec::new();
1148 /// let mut vec = Vec::with_capacity(10);
1149 /// vec.extend([1, 2, 3]);
1150 /// assert!(vec.capacity() >= 10);
1151 /// vec.shrink_to_fit();
1152 /// assert!(vec.capacity() >= 3);
1175 /// let mut vec = Vec::with_capacity(10);
1176 /// vec.extend([1, 2, 3]);
1177 /// assert!(vec.capacity() >= 10);
1178 /// vec.shrink_to(4);
1179 /// assert!(vec.capacity() >= 4);
1180 /// vec.shrink_to(0);
1181 /// assert!(vec.capacity() >= 3);
1201 /// let v = vec![1, 2, 3];
1209 /// let mut vec = Vec::with_capacity(10);
1210 /// vec.extend([1, 2, 3]);
1212 /// assert!(vec.capacity() >= 10);
1213 /// let slice = vec.into_boxed_slice();
1245 /// let mut vec = vec![1, 2, 3, 4, 5];
1246 /// vec.truncate(2);
1247 /// assert_eq!(vec, [1, 2]);
1254 /// let mut vec = vec![1, 2, 3];
1255 /// vec.truncate(8);
1256 /// assert_eq!(vec, [1, 2, 3]);
1263 /// let mut vec = vec![1, 2, 3];
1264 /// vec.truncate(0);
1265 /// assert_eq!(vec, []);
1268 /// [`clear`]: Vec::clear
1269 /// [`drain`]: Vec::drain
1301 /// let buffer = vec![1, 2, 3, 5, 8];
1318 /// let mut buffer = vec![0; 3];
1342 /// let x = vec![1, 2, 4];
1352 /// [`as_mut_ptr`]: Vec::as_mut_ptr
1374 /// let mut x: Vec<i32> = Vec::with_capacity(size);
1408 /// [`truncate`]: Vec::truncate
1409 /// [`resize`]: Vec::resize
1411 /// [`clear`]: Vec::clear
1418 /// [`capacity()`]: Vec::capacity
1439 /// pub fn get_dictionary(&self) -> Option<Vec<u8>> {
1441 /// let mut dict = Vec::with_capacity(32_768);
1466 /// let mut vec = vec![vec![1, 0, 0],
1467 /// vec![0, 1, 0],
1468 /// vec![0, 0, 1]];
1473 /// vec.set_len(0);
1494 /// [`remove`]: Vec::remove
1503 /// let mut v = vec!["foo", "bar", "baz", "qux"];
1546 /// let mut vec = vec![1, 2, 3];
1547 /// vec.insert(1, 4);
1548 /// assert_eq!(vec, [1, 4, 2, 3]);
1549 /// vec.insert(4, 5);
1550 /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1596 /// elements from the beginning of the `Vec`, consider using
1599 /// [`swap_remove`]: Vec::swap_remove
1609 /// let mut v = vec![1, 2, 3];
1654 /// let mut vec = vec![1, 2, 3, 4];
1655 /// vec.retain(|&x| x % 2 == 0);
1656 /// assert_eq!(vec, [2, 4]);
1663 /// let mut vec = vec![1, 2, 3, 4, 5];
1666 /// vec.retain(|_| *iter.next().unwrap());
1667 /// assert_eq!(vec, [2, 3, 5]);
1686 /// let mut vec = vec![1, 2, 3, 4];
1687 /// vec.retain_mut(|x| if *x <= 3 {
1693 /// assert_eq!(vec, [2, 3, 4]);
1705 // Vec: [Kept, Kept, Hole, Hole, Hole, Hole, Unchecked, Unchecked] in retain_mut()
1717 v: &'a mut Vec<T, A>, in retain_mut()
1797 /// let mut vec = vec![10, 20, 21, 30, 20];
1799 /// vec.dedup_by_key(|i| *i / 10);
1801 /// assert_eq!(vec, [10, 20, 30, 20]);
1825 /// let mut vec = vec!["foo", "bar", "Bar", "baz", "bar"];
1827 /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1829 /// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
1841 /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ in dedup_by()
1850 /* The Vec that would need correction if `same_bucket` panicked */ in dedup_by()
1851 vec: &'a mut Vec<T, A>, in dedup_by() field
1862 let ptr = self.vec.as_mut_ptr(); in dedup_by()
1863 let len = self.vec.len(); in dedup_by()
1866 * Basically vec[read..].len() */ in dedup_by()
1869 /* Pointer to first item in vec[write..write+items_left] slice */ in dedup_by()
1871 /* Pointer to first item in vec[read..] slice */ in dedup_by()
1874 /* Copy `vec[read..]` to `vec[write..write+items_left]`. in dedup_by()
1879 * Basically vec[read..write].len() */ in dedup_by()
1882 self.vec.set_len(len - dropped); in dedup_by()
1887 let mut gap = FillGapOnDrop { read: 1, write: 1, vec: self }; in dedup_by()
1888 let ptr = gap.vec.as_mut_ptr(); in dedup_by()
1890 /* Drop items while going through Vec, it should be more efficient than in dedup_by()
1922 gap.vec.set_len(gap.write); in dedup_by()
1936 /// let mut vec = vec![1, 2];
1937 /// vec.push(3);
1938 /// assert_eq!(vec, [1, 2, 3]);
1961 /// let mut vec = vec![1, 2];
1962 /// vec.try_push(3).unwrap();
1963 /// assert_eq!(vec, [1, 2, 3]);
1985 /// [`push`]: Vec::push
1986 /// [`reserve`]: Vec::reserve
1987 /// [`try_reserve`]: Vec::try_reserve
1997 /// fn from_iter_fallible<T>(iter: impl Iterator<Item=T>) -> Result<Vec<T>, TryReserveError> {
1998 /// let mut vec = Vec::new();
2000 /// if let Err(value) = vec.push_within_capacity(value) {
2001 /// vec.try_reserve(1)?;
2003 /// let _ = vec.push_within_capacity(value);
2006 /// Ok(vec)
2008 /// assert_eq!(from_iter_fallible(0..100), Ok(Vec::from_iter(0..100)));
2035 /// let mut vec = vec![1, 2, 3];
2036 /// assert_eq!(vec.pop(), Some(3));
2037 /// assert_eq!(vec, [1, 2]);
2061 /// let mut vec = vec![1, 2, 3];
2062 /// let mut vec2 = vec![4, 5, 6];
2063 /// vec.append(&mut vec2);
2064 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
2120 /// let mut v = vec![1, 2, 3];
2121 /// let u: Vec<_> = v.drain(1..).collect();
2141 // When finished, remaining tail of the vec is copied back to cover in drain()
2148 // set self.vec length's to start, to be safe in case Drain is leaked in drain()
2155 vec: NonNull::from(self), in drain()
2168 /// let mut v = vec![1, 2, 3];
2197 /// let a = vec![1, 2, 3];
2211 /// let mut v = Vec::new();
2235 /// let mut vec = vec![1, 2, 3];
2236 /// let vec2 = vec.split_off(1);
2237 /// assert_eq!(vec, [1]);
2262 Vec::with_capacity_in(self.capacity(), self.allocator().clone()), in split_off()
2267 let mut other = Vec::with_capacity_in(other_len, self.allocator().clone()); in split_off()
2279 /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
2281 /// If `new_len` is greater than `len`, the `Vec` is extended by the
2284 /// in the `Vec` in the order they have been generated.
2286 /// If `new_len` is less than `len`, the `Vec` is simply truncated.
2289 /// you'd rather [`Clone`] a given value, use [`Vec::resize`]. If you
2296 /// let mut vec = vec![1, 2, 3];
2297 /// vec.resize_with(5, Default::default);
2298 /// assert_eq!(vec, [1, 2, 3, 0, 0]);
2300 /// let mut vec = vec![];
2302 /// vec.resize_with(4, || { p *= 2; p });
2303 /// assert_eq!(vec, [2, 4, 8, 16]);
2319 /// Consumes and leaks the `Vec`, returning a mutable reference to the contents,
2324 /// As of Rust 1.57, this method does not reallocate or shrink the `Vec`,
2337 /// let x = vec![1, 2, 3];
2359 /// [`set_len`]: Vec::set_len
2365 /// let mut v = Vec::with_capacity(10);
2401 /// [`set_len`]: Vec::set_len
2404 /// optimization purposes. If you need to append data to a `Vec`
2409 /// [`push`]: Vec::push
2410 /// [`extend`]: Vec::extend
2411 /// [`extend_from_slice`]: Vec::extend_from_slice
2412 /// [`extend_from_within`]: Vec::extend_from_within
2413 /// [`insert`]: Vec::insert
2414 /// [`append`]: Vec::append
2415 /// [`resize`]: Vec::resize
2416 /// [`resize_with`]: Vec::resize_with
2423 /// let mut v = vec![1, 1, 2];
2456 /// This method provides unique access to all vec parts at once in `extend_from_within`.
2481 impl<T: Clone, A: Allocator> Vec<T, A> { impl
2482 /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
2484 /// If `new_len` is greater than `len`, the `Vec` is extended by the
2486 /// If `new_len` is less than `len`, the `Vec` is simply truncated.
2491 /// [`Clone`]), use [`Vec::resize_with`].
2492 /// If you only need to resize to a smaller size, use [`Vec::truncate`].
2497 /// let mut vec = vec!["hello"];
2498 /// vec.resize(3, "world");
2499 /// assert_eq!(vec, ["hello", "world", "world"]);
2501 /// let mut vec = vec![1, 2, 3, 4];
2502 /// vec.resize(2, 0);
2503 /// assert_eq!(vec, [1, 2]);
2517 /// Tries to resize the `Vec` in-place so that `len` is equal to `new_len`.
2519 /// If `new_len` is greater than `len`, the `Vec` is extended by the
2521 /// If `new_len` is less than `len`, the `Vec` is simply truncated.
2526 /// [`Clone`]), use [`Vec::resize_with`].
2527 /// If you only need to resize to a smaller size, use [`Vec::truncate`].
2532 /// let mut vec = vec!["hello"];
2533 /// vec.try_resize(3, "world").unwrap();
2534 /// assert_eq!(vec, ["hello", "world", "world"]);
2536 /// let mut vec = vec![1, 2, 3, 4];
2537 /// vec.try_resize(2, 0).unwrap();
2538 /// assert_eq!(vec, [1, 2]);
2540 /// let mut vec = vec![42];
2541 /// let result = vec.try_resize(usize::MAX, 0);
2556 /// Clones and appends all elements in a slice to the `Vec`.
2559 /// it to this `Vec`. The `other` slice is traversed in-order.
2569 /// let mut vec = vec![1];
2570 /// vec.extend_from_slice(&[2, 3, 4]);
2571 /// assert_eq!(vec, [1, 2, 3, 4]);
2574 /// [`extend`]: Vec::extend
2581 /// Tries to clone and append all elements in a slice to the `Vec`.
2584 /// it to this `Vec`. The `other` slice is traversed in-order.
2594 /// let mut vec = vec![1];
2595 /// vec.try_extend_from_slice(&[2, 3, 4]).unwrap();
2596 /// assert_eq!(vec, [1, 2, 3, 4]);
2599 /// [`extend`]: Vec::extend
2615 /// let mut vec = vec![0, 1, 2, 3, 4];
2617 /// vec.extend_from_within(2..);
2618 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
2620 /// vec.extend_from_within(..2);
2621 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
2623 /// vec.extend_from_within(4..8);
2624 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
2643 impl<T, A: Allocator, const N: usize> Vec<[T; N], A> { implementation
2644 /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`.
2659 /// let mut vec = vec![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
2660 /// assert_eq!(vec.pop(), Some([7, 8, 9]));
2662 /// let mut flattened = vec.into_flattened();
2666 pub fn into_flattened(self) -> Vec<T, A> { in into_flattened()
2669 (len.checked_mul(N).expect("vec len overflow"), usize::MAX) in into_flattened()
2684 unsafe { Vec::<T, A>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) } in into_flattened()
2688 impl<T: Clone, A: Allocator> Vec<T, A> { impl
2750 impl<T: PartialEq, A: Allocator> Vec<T, A> { impl
2759 /// let mut vec = vec![1, 2, 2, 3, 2];
2761 /// vec.dedup();
2763 /// assert_eq!(vec, [1, 2, 3, 2]);
2779 pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> { in from_elem()
2786 pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { in from_elem_in()
2798 impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { implementation
2817 impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { implementation
2846 // Common trait implementations for Vec
2850 impl<T, A: Allocator> ops::Deref for Vec<T, A> { implementation
2860 impl<T, A: Allocator> ops::DerefMut for Vec<T, A> { implementation
2869 impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { implementation
2898 /// let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
2903 impl<T: Hash, A: Allocator> Hash for Vec<T, A> { implementation
2915 impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> { implementation
2929 impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> { implementation
2938 impl<T> FromIterator<T> for Vec<T> { implementation
2940 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> { in from_iter()
2946 impl<T, A: Allocator> IntoIterator for Vec<T, A> { implementation
2957 /// let v = vec!["a".to_string(), "b".to_string()];
2991 impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> { implementation
3001 impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> { implementation
3012 impl<T, A: Allocator> Extend<T> for Vec<T, A> { implementation
3029 impl<T, A: Allocator> Vec<T, A> { impl
3175 /// let mut v = vec![1, 2, 3, 4];
3177 /// let u: Vec<_> = v.splice(1..3, new).collect();
3202 /// [`retain`]: Vec::retain
3208 /// # let mut vec = vec![1, 2, 3, 4, 5, 6];
3210 /// while i < vec.len() {
3211 /// if some_predicate(&mut vec[i]) {
3212 /// let val = vec.remove(i);
3219 /// # assert_eq!(vec, vec![1, 4, 5]);
3234 /// let mut numbers = vec![1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15];
3236 /// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
3239 /// assert_eq!(evens, vec![2, 4, 6, 8, 14]);
3240 /// assert_eq!(odds, vec![1, 3, 5, 9, 11, 13, 15]);
3254 ExtractIf { vec: self, idx: 0, del: 0, old_len, pred: filter } in extract_if()
3258 /// Extend implementation that copies elements out of references before pushing them onto the Vec.
3266 impl<'a, T: Copy + 'a, A: Allocator> Extend<&'a T> for Vec<T, A> { implementation
3284 impl<T, A1, A2> PartialOrd<Vec<T, A2>> for Vec<T, A1> implementation
3291 fn partial_cmp(&self, other: &Vec<T, A2>) -> Option<Ordering> { in partial_cmp()
3297 impl<T: Eq, A: Allocator> Eq for Vec<T, A> {} implementation
3301 impl<T: Ord, A: Allocator> Ord for Vec<T, A> { implementation
3309 unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> { implementation
3322 impl<T> Default for Vec<T> { implementation
3323 /// Creates an empty `Vec<T>`.
3326 fn default() -> Vec<T> { in default()
3327 Vec::new() in default()
3332 impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> { implementation
3339 impl<T, A: Allocator> AsRef<Vec<T, A>> for Vec<T, A> { implementation
3340 fn as_ref(&self) -> &Vec<T, A> { in as_ref()
3346 impl<T, A: Allocator> AsMut<Vec<T, A>> for Vec<T, A> { implementation
3347 fn as_mut(&mut self) -> &mut Vec<T, A> { in as_mut()
3353 impl<T, A: Allocator> AsRef<[T]> for Vec<T, A> { implementation
3360 impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> { implementation
3368 impl<T: Clone> From<&[T]> for Vec<T> { implementation
3369 /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
3374 /// assert_eq!(Vec::from(&[1, 2, 3][..]), vec![1, 2, 3]);
3377 fn from(s: &[T]) -> Vec<T> { in from()
3381 fn from(s: &[T]) -> Vec<T> { in from()
3388 impl<T: Clone> From<&mut [T]> for Vec<T> { implementation
3389 /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
3394 /// assert_eq!(Vec::from(&mut [1, 2, 3][..]), vec![1, 2, 3]);
3397 fn from(s: &mut [T]) -> Vec<T> { in from()
3401 fn from(s: &mut [T]) -> Vec<T> { in from()
3408 impl<T, const N: usize> From<[T; N]> for Vec<T> { implementation
3409 /// Allocate a `Vec<T>` and move `s`'s items into it.
3414 /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
3417 fn from(s: [T; N]) -> Vec<T> { in from()
3422 fn from(s: [T; N]) -> Vec<T> { in from()
3429 impl<'a, T> From<Cow<'a, [T]>> for Vec<T> implementation
3431 [T]: ToOwned<Owned = Vec<T>>,
3435 /// If `s` already owns a `Vec<T>`, it will be returned directly.
3436 /// If `s` is borrowing a slice, a new `Vec<T>` will be allocated and
3443 /// let o: Cow<'_, [i32]> = Cow::Owned(vec![1, 2, 3]);
3445 /// assert_eq!(Vec::from(o), Vec::from(b));
3447 fn from(s: Cow<'a, [T]>) -> Vec<T> { in from()
3455 impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> { implementation
3462 /// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice();
3463 /// assert_eq!(Vec::from(b), vec![1, 2, 3]);
3474 impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> {
3483 /// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
3488 /// let mut vec = Vec::with_capacity(10);
3489 /// vec.extend([1, 2, 3]);
3491 /// assert_eq!(Box::from(vec), vec![1, 2, 3].into_boxed_slice());
3493 fn from(v: Vec<T, A>) -> Self { in from()
3500 impl From<&str> for Vec<u8> { implementation
3501 /// Allocate a `Vec<u8>` and fill it with a UTF-8 string.
3506 /// assert_eq!(Vec::from("123"), vec![b'1', b'2', b'3']);
3508 fn from(s: &str) -> Vec<u8> { in from()
3514 impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] {
3515 type Error = Vec<T, A>;
3517 /// Gets the entire contents of the `Vec<T>` as an array,
3523 /// assert_eq!(vec![1, 2, 3].try_into(), Ok([1, 2, 3]));
3524 /// assert_eq!(<Vec<i32>>::new().try_into(), Ok([]));
3529 /// let r: Result<[i32; 4], _> = (0..10).collect::<Vec<_>>().try_into();
3530 /// assert_eq!(r, Err(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
3533 /// If you're fine with just getting a prefix of the `Vec<T>`,
3534 /// you can call [`.truncate(N)`](Vec::truncate) first.
3543 fn try_from(mut vec: Vec<T, A>) -> Result<[T; N], Vec<T, A>> { in try_from()
3544 if vec.len() != N {
3545 return Err(vec);
3549 unsafe { vec.set_len(0) };
3551 // SAFETY: A `Vec`'s pointer is always aligned properly, and
3555 // tells the `Vec` not to also drop them.
3556 let array = unsafe { ptr::read(vec.as_ptr() as *const [T; N]) };