-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0359_logger_rate_limiter.rs
More file actions
37 lines (34 loc) · 1.07 KB
/
_0359_logger_rate_limiter.rs
File metadata and controls
37 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::collections::HashMap;
#[derive(Default)]
struct Logger {
log: HashMap<String, i32>,
}
impl Logger {
fn new() -> Self {
Logger {
log: HashMap::new(),
}
}
fn should_print_message(&mut self, timestamp: i32, message: String) -> bool {
if let Some(&v) = self.log.get(&message) {
if timestamp - v >= 10 {
self.log.insert(message, timestamp);
} else {
return false;
}
} else {
self.log.insert(message, timestamp);
}
return true;
}
}
#[test]
fn test() {
let mut logger = Logger::new();
assert_eq!(logger.should_print_message(1, "foo".to_string()), true);
assert_eq!(logger.should_print_message(2, "bar".to_string()), true);
assert_eq!(logger.should_print_message(3, "foo".to_string()), false);
assert_eq!(logger.should_print_message(8, "bar".to_string()), false);
assert_eq!(logger.should_print_message(10, "foo".to_string()), false);
assert_eq!(logger.should_print_message(11, "foo".to_string()), true);
}