Skip to content

Commit e13fe23

Browse files
add tests
1 parent 128b441 commit e13fe23

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,14 +2236,12 @@ impl<T, A: Allocator> Vec<T, A> {
22362236
/// # Examples
22372237
///
22382238
/// ```
2239-
/// let mut v = vec!['a', 'b', 'c'];
2240-
/// assert_eq!(v.try_remove(1), Some('b'));
2241-
/// assert_eq!(v, ['a', 'c']);
2242-
///
2239+
/// #![feature(vec_try_remove)]
2240+
/// let mut v = vec![1, 2, 3];
2241+
/// assert_eq!(v.try_remove(0), Some(1));
22432242
/// assert_eq!(v.try_remove(2), None);
2244-
/// assert_eq!(v, ['a', 'c']);
22452243
/// ```
2246-
#[stable(feature = "vec_try_remove", since = "1.92.0")]
2244+
#[unstable(feature = "vec_try_remove", issue = "77481")]
22472245
#[track_caller]
22482246
#[rustc_confusables("delete", "take", "remove")]
22492247
pub fn try_remove(&mut self, index: usize) -> Option<T> {

library/alloctests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#![feature(unique_rc_arc)]
4343
#![feature(macro_metavar_expr_concat)]
4444
#![feature(vec_peek_mut)]
45+
#![feature(vec_try_remove)]
4546
#![allow(internal_features)]
4647
#![deny(fuzzy_provenance_casts)]
4748
#![deny(unsafe_op_in_unsafe_fn)]

library/alloctests/tests/vec.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,21 @@ fn test_swap_remove_empty() {
630630
vec.swap_remove(0);
631631
}
632632

633+
#[test]
634+
fn test_try_remove() {
635+
let mut vec = vec![1, 2, 3];
636+
// We are attempting to remove vec[0] which contains 1
637+
assert_eq!(vec.try_remove(0), Some(1));
638+
// Now `vec` looks like: [2, 3]
639+
// We will now try to remove vec[2] which does not exist
640+
// This should return `None`
641+
assert_eq!(vec.try_remove(2), None);
642+
643+
// We will try the same thing with an empty vector
644+
let mut v: Vec<u8> = vec![];
645+
assert!(v.try_remove(0).is_none());
646+
}
647+
633648
#[test]
634649
fn test_move_items() {
635650
let vec = vec![1, 2, 3];

0 commit comments

Comments
 (0)