Is this splice test correct?

โš“ Rust    ๐Ÿ“… 2026-02-25    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

I just want to check my understanding here.

I am now starting to test my Vec implementation using the standard tests, one test is

#[test]
fn test_splice_forget() {
    let mut v = vec![1, 2, 3, 4, 5];
    let a = [10, 11, 12];
    std::mem::forget(v.splice(2..4, a));
    assert_eq!(v, &[1, 2]);
}

But the documentation says:

It is unspecified how many elements are removed from the vector if the Splice value is leaked.

So how is that test valid? Or is there a sort of hidden specification on how it โ€œreally worksโ€, or perhaps the test is making sure the way it works doesnโ€™t change?

3 posts - 2 participants

Read full topic

๐Ÿท๏ธ Rust_feed