Решение на упр.03 задача 3 от Илиян Копривчин

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

Към профила на Илиян Копривчин

Резултати

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

Код

use std::collections::HashMap;
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 },
}
struct EventLog { events: Vec<Event> }
impl EventLog {
fn new() -> EventLog {
EventLog {events: Vec::new()}
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, user: &str) -> f64 {
let mut total = 0.0;
for event in &self.events {
if let Event::Purchase { user: u, amount, .. } = event {
if u == user {
total += amount;
}
}
}
return total;
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut map:HashMap<String, usize> = HashMap::new();
for event in &self.events {
let key = match event {
Event::Login { .. } => "Login",
Event::Logout { .. } => "Logout",
Event::Purchase { .. } => "Purchase",
Event::Error { .. } => "Error",
};
let entry = map.entry(key.to_string()).or_insert(0);
*entry += 1;
}
return map;
}
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut result: Vec<&Event> = Vec::new();
for event in &self.events {
match event {
Event::Login { user: u, timestamp }
| Event::Logout { user: u, timestamp }
| Event::Purchase { user: u, timestamp, .. } => {
let user_ok = match user {
Some(name) => u == name,
None => true,
};
let time_ok = match after {
Some(t) => *timestamp > t,
None => true,
};
if user_ok && time_ok {
result.push(event);
}
}
Event::Error { .. } => {}
}
}
return result;
}
}

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

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.41
   Compiling unicode-ident v1.0.22
   Compiling futures-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling syn v2.0.108
   Compiling slab v0.4.11
   Compiling memchr v2.7.6
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-ou8rvh/solution)
warning: enum `Event` is never used
 --> src/lib.rs:3:6
  |
3 | enum Event {
  |      ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: struct `EventLog` is never constructed
  --> src/lib.rs:10:8
   |
10 | struct EventLog { events: Vec<Event> }
   |        ^^^^^^^^

warning: associated items `new`, `add_event`, `user_spent`, `summaries_by_type`, and `filter_events` are never used
  --> src/lib.rs:13:8
   |
12 | impl EventLog {
   | ------------- associated items in this implementation
13 |     fn new() -> EventLog {
   |        ^^^
...
17 |     fn add_event(&mut self, event: Event) {
   |        ^^^^^^^^^
...
21 |     fn user_spent(&self, user: &str) -> f64 {
   |        ^^^^^^^^^^
...
35 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
...
53 |     fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
   |        ^^^^^^^^^^^^^

warning: `solution` (lib) generated 3 warnings
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.61s
     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 23:50 (преди 1 ден)