@@ -9,11 +9,12 @@ of the values on it at any time.
9
9
## Examples
10
10
11
11
``` rust
12
+ use std :: time :: Duration ;
13
+ use std :: thread;
12
14
use sum_queue :: SumQueue ;
13
- use std :: {time, thread};
14
15
15
16
// creates a queue where elements expire after 2 seconds
16
- let mut queue : SumQueue <i32 > = SumQueue :: new (2 );
17
+ let mut queue : SumQueue <i32 > = SumQueue :: new (Duration :: from_secs ( 2 ) );
17
18
queue . push (1 );
18
19
queue . push (10 );
19
20
queue . push (3 );
@@ -42,20 +43,21 @@ println!("Stats - length of queue: {}", stats.len); // 3
42
43
43
44
assert_eq! (queue . pop (), Some (1 ));
44
45
assert_eq! (queue . iter (). collect :: <Vec <_ >>(), vec! [& 5 , & 2 ]);
46
+ println! (" Elements after pop: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2]
45
47
46
48
// After a second the elements are still the same
47
- thread :: sleep (time :: Duration :: from_secs (1 ));
48
- println! (" Same elements : {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2]
49
+ thread :: sleep (Duration :: from_secs (1 ));
50
+ println! (" Same after 1 sec : {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2]
49
51
50
52
queue . push (50 ); // Add an element 1 second younger than the rest of elements
51
53
println! (" Same elements + 50: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2, 50]
52
54
53
- // Now let sleep 2 secs so the first elements expire
54
- thread :: sleep (time :: Duration :: from_secs (2 ));
55
+ // Now let sleep 1 sec so the first elements expire
56
+ thread :: sleep (Duration :: from_secs (1 ));
55
57
println! (" Just 50: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [50]
56
58
57
- // 2 seconds later the last element also expires
58
- thread :: sleep (time :: Duration :: from_secs (2 ));
59
+ // 1 second more later the last element also expires
60
+ thread :: sleep (Duration :: from_secs (1 ));
59
61
println! (" No elements: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // []
60
62
```
61
63
0 commit comments