@@ -2032,27 +2032,24 @@ impl<T, A: Allocator> Vec<T, A> {
2032
2032
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2033
2033
#[ track_caller]
2034
2034
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) ;
2038
2036
}
2039
2037
2040
2038
/// Inserts an element at position `index` within the vector, shifting all
2041
2039
/// elements after it to the right.
2042
2040
///
2043
- /// Returns [`None`] if the specified index is out of bounds.
2041
+ /// # Panics
2042
+ ///
2043
+ /// Panics if `index > len`.
2044
2044
///
2045
2045
/// # Examples
2046
2046
///
2047
2047
/// ```
2048
2048
/// #![feature(push_mut)]
2049
2049
/// 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);
2051
2051
/// *x += 1;
2052
2052
/// assert_eq!(vec, [1, 3, 5, 7, 9]);
2053
- ///
2054
- /// let y = vec.insert_mut(7, 6);
2055
- /// assert!(y.is_none());
2056
2053
/// ```
2057
2054
///
2058
2055
/// # Time complexity
@@ -2064,12 +2061,12 @@ impl<T, A: Allocator> Vec<T, A> {
2064
2061
#[ inline]
2065
2062
#[ unstable( feature = "push_mut" , issue = "135974" ) ]
2066
2063
#[ 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 {
2069
2066
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
+ } ;
2073
2070
2074
2071
// space for the new element
2075
2072
if len == self . buf . capacity ( ) {
@@ -2091,7 +2088,7 @@ impl<T, A: Allocator> Vec<T, A> {
2091
2088
ptr:: write ( p, element) ;
2092
2089
}
2093
2090
self . set_len ( len + 1 ) ;
2094
- Some ( & mut * p)
2091
+ & mut * p
2095
2092
}
2096
2093
}
2097
2094
0 commit comments