| 1 | use defer_droprust-analyzer cargo defer-drop 1.3.0 crate/::DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeat_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat_with/repeat_with().; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::{Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#}; |
| 4 | |
| 5 | fn timerrust-analyzer cargo defer-drop 1.3.0 timer().(f: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#()) -> Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# { |
| 6 | let start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 7 | f(); |
| 8 | start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().() |
| 9 | } |
| 10 | |
| 11 | fn mainrust-analyzer cargo defer-drop 1.3.0 main().() { |
| 12 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Allocating a ridiculously large vector"); |
| 13 | let vec1: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> = repeat_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat_with/repeat_with().(|| { |
| 14 | repeat_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat_with/repeat_with().(|| "Hello, World".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 15 | .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(1000) |
| 16 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 17 | }) |
| 18 | .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(1000) |
| 19 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 20 | |
| 21 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Duplicating that vector"); |
| 22 | let vec2 = vec1.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(); |
| 23 | let defer_vec1 = DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(vec1); |
| 24 | |
| 25 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Dropping the vectors"); |
| 26 | |
| 27 | let vec1_timer = timerrust-analyzer cargo defer-drop 1.3.0 timer().(move || droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(defer_vec1)); |
| 28 | let vec2_timer = timerrust-analyzer cargo defer-drop 1.3.0 timer().(move || droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(vec2)); |
| 29 | |
| 30 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Duration of deferred drop: {:?}", vec1_timer); |
| 31 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Duration of foreground drop: {:?}", vec2_timer); |
| 32 | } |