Skip to content

Commit b1a46db

Browse files
committed
Rollup merge of rust-lang#22247 - Gankro:dlist_split, r=alexcrichton
2 parents 97503e1 + 1c49404 commit b1a46db

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/libcollections/dlist.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,12 @@ impl<T> DList<T> {
560560
/// Splits the list into two at the given index. Returns everything after the given index,
561561
/// including the index.
562562
///
563+
/// # Panics
564+
///
565+
/// Panics if `at > len`.
566+
///
563567
/// This operation should compute in O(n) time.
568+
///
564569
/// # Examples
565570
///
566571
/// ```
@@ -580,9 +585,11 @@ impl<T> DList<T> {
580585
#[stable(feature = "rust1", since = "1.0.0")]
581586
pub fn split_off(&mut self, at: usize) -> DList<T> {
582587
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");
584589
if at == 0 {
585590
return mem::replace(self, DList::new());
591+
} else if at == len {
592+
return DList::new();
586593
}
587594

588595
// Below, we iterate towards the `i-1`th node, either from the start or the end,
@@ -1116,6 +1123,18 @@ mod tests {
11161123
}
11171124
}
11181125

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+
11191138
}
11201139

11211140
#[test]

0 commit comments

Comments
 (0)