@@ -560,7 +560,12 @@ impl<T> DList<T> {
560
560
/// Splits the list into two at the given index. Returns everything after the given index,
561
561
/// including the index.
562
562
///
563
+ /// # Panics
564
+ ///
565
+ /// Panics if `at > len`.
566
+ ///
563
567
/// This operation should compute in O(n) time.
568
+ ///
564
569
/// # Examples
565
570
///
566
571
/// ```
@@ -580,9 +585,11 @@ impl<T> DList<T> {
580
585
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
581
586
pub fn split_off ( & mut self , at : usize ) -> DList < T > {
582
587
let len = self . len ( ) ;
583
- assert ! ( at < len, "Cannot split off at a nonexistent index" ) ;
588
+ assert ! ( at <= len, "Cannot split off at a nonexistent index" ) ;
584
589
if at == 0 {
585
590
return mem:: replace ( self , DList :: new ( ) ) ;
591
+ } else if at == len {
592
+ return DList :: new ( ) ;
586
593
}
587
594
588
595
// Below, we iterate towards the `i-1`th node, either from the start or the end,
@@ -1116,6 +1123,18 @@ mod tests {
1116
1123
}
1117
1124
}
1118
1125
1126
+ // no-op on the last index
1127
+ {
1128
+ let mut m = DList :: new ( ) ;
1129
+ m. push_back ( 1 ) ;
1130
+
1131
+ let p = m. split_off ( 1 ) ;
1132
+ assert_eq ! ( m. len( ) , 1 ) ;
1133
+ assert_eq ! ( p. len( ) , 0 ) ;
1134
+ assert_eq ! ( m. back( ) , Some ( & 1 ) ) ;
1135
+ assert_eq ! ( m. front( ) , Some ( & 1 ) ) ;
1136
+ }
1137
+
1119
1138
}
1120
1139
1121
1140
#[ test]
0 commit comments