< 返回版块

douchuan 发表于 2021-08-29 15:14

很漂亮的一段case,来自std

library/alloc/tests/linked_list.rs

#[test]
fn test_drop() {
    static mut DROPS: i32 = 0;
    struct Elem;
    impl Drop for Elem {
        fn drop(&mut self) {
            unsafe {
                DROPS += 1;
            }
        }
    }

    let mut ring = LinkedList::new();
    ring.push_back(Elem);
    ring.push_front(Elem);
    ring.push_back(Elem);
    ring.push_front(Elem);
    drop(ring);

    assert_eq!(unsafe { DROPS }, 4);
}

评论区

写评论

还没有评论

1 共 0 条评论, 1 页