Skip to content

Commit 7ef782f

Browse files
authored
Update mod.rs
1 parent e269c85 commit 7ef782f

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,27 +2032,24 @@ impl<T, A: Allocator> Vec<T, A> {
20322032
#[stable(feature = "rust1", since = "1.0.0")]
20332033
#[track_caller]
20342034
pub fn insert(&mut self, index: usize, element: T) {
2035-
if intrinsics::unlikely(self.insert_mut(index, element).is_none()) {
2036-
panic!("insertion index (is {index}) should be <= len (is {})", self.len())
2037-
};
2035+
let _ = self.insert_mut(index, element);
20382036
}
20392037

20402038
/// Inserts an element at position `index` within the vector, shifting all
20412039
/// elements after it to the right.
20422040
///
2043-
/// Returns [`None`] if the specified index is out of bounds.
2041+
/// # Panics
2042+
///
2043+
/// Panics if `index > len`.
20442044
///
20452045
/// # Examples
20462046
///
20472047
/// ```
20482048
/// #![feature(push_mut)]
20492049
/// let mut vec = vec![1, 3, 5, 9];
2050-
/// let x = vec.insert_mut(3, 6).unwrap();
2050+
/// let x = vec.insert_mut(3, 6);
20512051
/// *x += 1;
20522052
/// assert_eq!(vec, [1, 3, 5, 7, 9]);
2053-
///
2054-
/// let y = vec.insert_mut(7, 6);
2055-
/// assert!(y.is_none());
20562053
/// ```
20572054
///
20582055
/// # Time complexity
@@ -2064,12 +2061,12 @@ impl<T, A: Allocator> Vec<T, A> {
20642061
#[inline]
20652062
#[unstable(feature = "push_mut", issue = "135974")]
20662063
#[track_caller]
2067-
#[must_use = "if you don't need a reference to the value or type-safe bound checking, use Vec::insert instead"]
2068-
pub fn insert_mut(&mut self, index: usize, element: T) -> Option<&mut T> {
2064+
#[must_use = "if you don't need a reference to the value, use Vec::insert instead"]
2065+
pub fn insert_mut(&mut self, index: usize, element: T) -> &mut T {
20692066
let len = self.len();
2070-
if index > len {
2071-
return None;
2072-
}
2067+
if intrinsics::unlikely(index > len) {
2068+
panic!("insertion index (is {index}) should be <= len (is {})", len)
2069+
};
20732070

20742071
// space for the new element
20752072
if len == self.buf.capacity() {
@@ -2091,7 +2088,7 @@ impl<T, A: Allocator> Vec<T, A> {
20912088
ptr::write(p, element);
20922089
}
20932090
self.set_len(len + 1);
2094-
Some(&mut *p)
2091+
&mut *p
20952092
}
20962093
}
20972094

0 commit comments

Comments
 (0)