@@ -9,11 +9,12 @@ of the values on it at any time.
99## Examples
1010
1111``` rust
12+ use std :: time :: Duration ;
13+ use std :: thread;
1214use sum_queue :: SumQueue ;
13- use std :: {time, thread};
1415
1516// 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 ) );
1718queue . push (1 );
1819queue . push (10 );
1920queue . push (3 );
@@ -42,20 +43,21 @@ println!("Stats - length of queue: {}", stats.len); // 3
4243
4344assert_eq! (queue . pop (), Some (1 ));
4445assert_eq! (queue . iter (). collect :: <Vec <_ >>(), vec! [& 5 , & 2 ]);
46+ println! (" Elements after pop: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2]
4547
4648// 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]
4951
5052queue . push (50 ); // Add an element 1 second younger than the rest of elements
5153println! (" Same elements + 50: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [5, 2, 50]
5254
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 ));
5557println! (" Just 50: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // [50]
5658
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 ));
5961println! (" No elements: {:?}" , queue . iter (). collect :: <Vec <_ >>()); // []
6062```
6163
0 commit comments