Решение на упр.03 задача 3 от Станислав Стаматов

Обратно към всички решения

Към профила на Станислав Стаматов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 2 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::collections::HashMap;
#[derive(Debug)]
enum Event {
Login {
user: String,
timestamp: u64,
},
Logout {
user: String,
timestamp: u64,
},
Purchase {
user: String,
item: String,
amount: f64,
timestamp: u64,
},
Error {
code: i32,
message: String,
timestamp: u64,
},
}
impl Event {
pub fn timestamp(&self) -> u64 {
match self {
Event::Login { user, timestamp } => *timestamp,
Event::Logout { user, timestamp } => *timestamp,
Event::Purchase {
user,
item,
amount,
timestamp,
} => *timestamp,
Event::Error {
code,
message,
timestamp,
} => *timestamp,
}
}
}
#[derive(Debug)]
struct EventLog {
events: Vec<Event>,
}
impl EventLog {
pub fn new() -> Self {
Self { events: Vec::new() }
}
pub fn add_event(&mut self, event: Event) {
self.events.push(event)
}
pub fn user_spent(&self, target_user: &str) -> f64 {
let mut sum: f64 = 0.0;
// self.events.iter().filter(|el| )
for event in self.events.iter() {
match event {
Event::Purchase {
user,
item,
amount,
timestamp,
} => {
if user == target_user {
sum += amount;
}
}
default => {}
}
}
sum
}
pub fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut summary: HashMap<String, usize> = HashMap::new();
for event in self.events.iter() {
match event {
Event::Login { user, timestamp } => {
summary
.entry(String::from("Login"))
.and_modify(|e| *e += 1)
.or_insert(1);
}
Event::Logout { user, timestamp } => {
summary
.entry(String::from("Logout"))
.and_modify(|e| *e += 1)
.or_insert(1);
}
Event::Purchase {
user,
item,
amount,
timestamp,
} => {
summary
.entry(String::from("Purchase"))
.and_modify(|e| *e += 1)
.or_insert(1);
}
Event::Error {
code,
message,
timestamp,
} => {
summary
.entry(String::from("Error"))
.and_modify(|e| *e += 1)
.or_insert(1);
}
}
}
summary
}
pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut result_ev: Vec<&Event> = self.events.iter().clone().collect();
if let Some(target_user) = user {
result_ev = result_ev
.iter()
.filter(|event| match event {
Event::Login { user, timestamp } => return user == target_user,
Event::Logout { user, timestamp } => return user == target_user,
Event::Purchase {
user,
item,
amount,
timestamp,
} => return user == target_user,
Event::Error {
code,
message,
timestamp,
} => return false,
})
.map(|event| *event)
.collect::<Vec<&Event>>();
}
if let Some(after) = after {
result_ev = result_ev
.iter()
.filter(|event| event.timestamp() > after)
.map(|event| *event)
.collect::<Vec<&Event>>();
}
result_ev
}
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.41
   Compiling futures-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling futures-io v0.3.31
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-qk1b3l/solution)
warning: unused variable: `user`
  --> src/lib.rs:29:28
   |
29 |             Event::Login { user, timestamp } => *timestamp,
   |                            ^^^^ help: try ignoring the field: `user: _`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `user`
  --> src/lib.rs:30:29
   |
30 |             Event::Logout { user, timestamp } => *timestamp,
   |                             ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `user`
  --> src/lib.rs:32:17
   |
32 |                 user,
   |                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> src/lib.rs:33:17
   |
33 |                 item,
   |                 ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> src/lib.rs:34:17
   |
34 |                 amount,
   |                 ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `code`
  --> src/lib.rs:38:17
   |
38 |                 code,
   |                 ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
  --> src/lib.rs:39:17
   |
39 |                 message,
   |                 ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `item`
  --> src/lib.rs:64:21
   |
64 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:66:21
   |
66 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `default`
  --> src/lib.rs:72:17
   |
72 |                 default => {}
   |                 ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_default`

warning: unused variable: `user`
  --> src/lib.rs:81:32
   |
81 |                 Event::Login { user, timestamp } => {
   |                                ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:81:38
   |
81 |                 Event::Login { user, timestamp } => {
   |                                      ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:87:33
   |
87 |                 Event::Logout { user, timestamp } => {
   |                                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:87:39
   |
87 |                 Event::Logout { user, timestamp } => {
   |                                       ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:94:21
   |
94 |                     user,
   |                     ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> src/lib.rs:95:21
   |
95 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> src/lib.rs:96:21
   |
96 |                     amount,
   |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:97:21
   |
97 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `code`
   --> src/lib.rs:105:21
    |
105 |                     code,
    |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> src/lib.rs:106:21
    |
106 |                     message,
    |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
   --> src/lib.rs:107:21
    |
107 |                     timestamp,
    |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `timestamp`
   --> src/lib.rs:125:42
    |
125 |                     Event::Login { user, timestamp } => return user == target_user,
    |                                          ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `timestamp`
   --> src/lib.rs:126:43
    |
126 |                     Event::Logout { user, timestamp } => return user == target_user,
    |                                           ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `item`
   --> src/lib.rs:129:25
    |
129 |                         item,
    |                         ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
   --> src/lib.rs:130:25
    |
130 |                         amount,
    |                         ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
   --> src/lib.rs:131:25
    |
131 |                         timestamp,
    |                         ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `code`
   --> src/lib.rs:134:25
    |
134 |                         code,
    |                         ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> src/lib.rs:135:25
    |
135 |                         message,
    |                         ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
   --> src/lib.rs:136:25
    |
136 |                         timestamp,
    |                         ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: enum `Event` is never used
 --> src/lib.rs:4:6
  |
4 | enum Event {
  |      ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `timestamp` is never used
  --> src/lib.rs:27:12
   |
26 | impl Event {
   | ---------- method in this implementation
27 |     pub fn timestamp(&self) -> u64 {
   |            ^^^^^^^^^

warning: struct `EventLog` is never constructed
  --> src/lib.rs:47:8
   |
47 | struct EventLog {
   |        ^^^^^^^^

warning: associated items `new`, `add_event`, `user_spent`, `summaries_by_type`, and `filter_events` are never used
   --> src/lib.rs:51:12
    |
50  | impl EventLog {
    | ------------- associated items in this implementation
51  |     pub fn new() -> Self {
    |            ^^^
...
54  |     pub fn add_event(&mut self, event: Event) {
    |            ^^^^^^^^^
...
57  |     pub fn user_spent(&self, target_user: &str) -> f64 {
    |            ^^^^^^^^^^
...
77  |     pub fn summaries_by_type(&self) -> HashMap<String, usize> {
    |            ^^^^^^^^^^^^^^^^^
...
118 |     pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
    |            ^^^^^^^^^^^^^

warning: `solution` (lib) generated 33 warnings
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: unused variable: `user`
  --> tests/../src/lib.rs:29:28
   |
29 |             Event::Login { user, timestamp } => *timestamp,
   |                            ^^^^ help: try ignoring the field: `user: _`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `user`
  --> tests/../src/lib.rs:30:29
   |
30 |             Event::Logout { user, timestamp } => *timestamp,
   |                             ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:32:17
   |
32 |                 user,
   |                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> tests/../src/lib.rs:33:17
   |
33 |                 item,
   |                 ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> tests/../src/lib.rs:34:17
   |
34 |                 amount,
   |                 ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `code`
  --> tests/../src/lib.rs:38:17
   |
38 |                 code,
   |                 ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
  --> tests/../src/lib.rs:39:17
   |
39 |                 message,
   |                 ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `item`
  --> tests/../src/lib.rs:64:21
   |
64 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:66:21
   |
66 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `default`
  --> tests/../src/lib.rs:72:17
   |
72 |                 default => {}
   |                 ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_default`

warning: unused variable: `user`
  --> tests/../src/lib.rs:81:32
   |
81 |                 Event::Login { user, timestamp } => {
   |                                ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:81:38
   |
81 |                 Event::Login { user, timestamp } => {
   |                                      ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:87:33
   |
87 |                 Event::Logout { user, timestamp } => {
   |                                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:87:39
   |
87 |                 Event::Logout { user, timestamp } => {
   |                                       ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:94:21
   |
94 |                     user,
   |                     ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> tests/../src/lib.rs:95:21
   |
95 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> tests/../src/lib.rs:96:21
   |
96 |                     amount,
   |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:97:21
   |
97 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `code`
   --> tests/../src/lib.rs:105:21
    |
105 |                     code,
    |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> tests/../src/lib.rs:106:21
    |
106 |                     message,
    |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
   --> tests/../src/lib.rs:107:21
    |
107 |                     timestamp,
    |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `timestamp`
   --> tests/../src/lib.rs:125:42
    |
125 |                     Event::Login { user, timestamp } => return user == target_user,
    |                                          ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `timestamp`
   --> tests/../src/lib.rs:126:43
    |
126 |                     Event::Logout { user, timestamp } => return user == target_user,
    |                                           ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `item`
   --> tests/../src/lib.rs:129:25
    |
129 |                         item,
    |                         ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
   --> tests/../src/lib.rs:130:25
    |
130 |                         amount,
    |                         ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
   --> tests/../src/lib.rs:131:25
    |
131 |                         timestamp,
    |                         ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `code`
   --> tests/../src/lib.rs:134:25
    |
134 |                         code,
    |                         ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> tests/../src/lib.rs:135:25
    |
135 |                         message,
    |                         ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
   --> tests/../src/lib.rs:136:25
    |
136 |                         timestamp,
    |                         ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: `solution` (test "solution_test") generated 29 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.63s
     Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)

running 2 tests
test solution_test::test_empty ... ok
test solution_test::test_basic ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

История (1 версия и 0 коментара)

Станислав качи първо решение на 29.10.2025 11:24 (преди 1 ден)