diff options
| author | BenjaminBrienen <Benjamin.Brienen@outlook.com> | 2025-09-24 14:25:10 +0200 |
|---|---|---|
| committer | BenjaminBrienen <Benjamin.Brienen@outlook.com> | 2025-09-24 15:29:09 +0200 |
| commit | 56734495e2fd485a7be1ba77da1bf18a0eca4636 (patch) | |
| tree | 967f670cbe6adffe4d04c9b29299a4efe92db2e2 /library/alloctests | |
| parent | 3e887f5faa673bd1f9f6c95fc5d201598818add4 (diff) | |
| download | rust-56734495e2fd485a7be1ba77da1bf18a0eca4636.tar.gz rust-56734495e2fd485a7be1ba77da1bf18a0eca4636.zip | |
feature: Implement vec_try_remove
Vec::try_remove is a non-panicking version of Vec::remove
Diffstat (limited to 'library/alloctests')
| -rw-r--r-- | library/alloctests/tests/lib.rs | 1 | ||||
| -rw-r--r-- | library/alloctests/tests/vec.rs | 15 |
2 files changed, 16 insertions, 0 deletions
diff --git a/library/alloctests/tests/lib.rs b/library/alloctests/tests/lib.rs index 8c3ce156f3c..cba9883e148 100644 --- a/library/alloctests/tests/lib.rs +++ b/library/alloctests/tests/lib.rs @@ -41,6 +41,7 @@ #![feature(unique_rc_arc)] #![feature(macro_metavar_expr_concat)] #![feature(vec_peek_mut)] +#![feature(vec_try_remove)] #![allow(internal_features)] #![deny(fuzzy_provenance_casts)] #![deny(unsafe_op_in_unsafe_fn)] diff --git a/library/alloctests/tests/vec.rs b/library/alloctests/tests/vec.rs index 33a34daccbf..ea334ab0f14 100644 --- a/library/alloctests/tests/vec.rs +++ b/library/alloctests/tests/vec.rs @@ -631,6 +631,21 @@ fn test_swap_remove_empty() { } #[test] +fn test_try_remove() { + let mut vec = vec![1, 2, 3]; + // We are attempting to remove vec[0] which contains 1 + assert_eq!(vec.try_remove(0), Some(1)); + // Now `vec` looks like: [2, 3] + // We will now try to remove vec[2] which does not exist + // This should return `None` + assert_eq!(vec.try_remove(2), None); + + // We will try the same thing with an empty vector + let mut v: Vec<u8> = vec![]; + assert!(v.try_remove(0).is_none()); +} + +#[test] fn test_move_items() { let vec = vec![1, 2, 3]; let mut vec2 = vec![]; |
