Решение на упр.03 задача 3 от Константин Илиев
Към профила на Константин Илиев
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 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 {
        self.events.iter().filter_map(|e| {
            if let Event::Purchase { user: u, amount, .. } = e {
                if u == user { Some(*amount) } else { None }
            } else {
                None
            }
        }).sum()
    }
    fn summaries_by_type(&self) -> HashMap<String, usize> {
        let mut map = HashMap::new();
        for e in &self.events {
            let key = match e {
                Event::Login   { .. } => "Login",
                Event::Logout  { .. } => "Logout",
                Event::Purchase{ .. } => "Purchase",
                Event::Error   { .. } => "Error",
            };
            *map.entry(key.to_string()).or_insert(0) += 1;
        }
        map
    }
}
Лог от изпълнението
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-io v0.3.31
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-714c0g/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`, and `summaries_by_type` 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 {
   |        ^^^^^^^^^^
...
31 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
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
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
  --> tests/solution_test.rs:14:26
   |
14 |     assert!(matches!(log.filter_events(None, None).as_slice(), &[]));
   |                          ^^^^^^^^^^^^^ method not found in `EventLog`
   |
  ::: tests/../src/lib.rs:10:1
   |
10 | struct EventLog{ events: Vec<Event>, }
   | --------------- method `filter_events` not found for this struct
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
  --> tests/solution_test.rs:15:26
   |
15 |     assert!(matches!(log.filter_events(Some("Пешо"), None).as_slice(), &[]));
   |                          ^^^^^^^^^^^^^ method not found in `EventLog`
   |
  ::: tests/../src/lib.rs:10:1
   |
10 | struct EventLog{ events: Vec<Event>, }
   | --------------- method `filter_events` not found for this struct
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
  --> tests/solution_test.rs:16:26
   |
16 |     assert!(matches!(log.filter_events(None, Some(100)).as_slice(), &[]));
   |                          ^^^^^^^^^^^^^ method not found in `EventLog`
   |
  ::: tests/../src/lib.rs:10:1
   |
10 | struct EventLog{ events: Vec<Event>, }
   | --------------- method `filter_events` not found for this struct
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
  --> tests/solution_test.rs:86:28
   |
86 |         let filtered = log.filter_events(Some("Пешо"), None);
   |                            ^^^^^^^^^^^^^ method not found in `EventLog`
   |
  ::: tests/../src/lib.rs:10:1
   |
10 | struct EventLog{ events: Vec<Event>, }
   | --------------- method `filter_events` not found for this struct
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
   --> tests/solution_test.rs:120:28
    |
120 |         let filtered = log.filter_events(None, Some(139));
    |                            ^^^^^^^^^^^^^ method not found in `EventLog`
    |
   ::: tests/../src/lib.rs:10:1
    |
10  | struct EventLog{ events: Vec<Event>, }
    | --------------- method `filter_events` not found for this struct
error[E0599]: no method named `filter_events` found for struct `EventLog` in the current scope
   --> tests/solution_test.rs:143:28
    |
143 |         let filtered = log.filter_events(Some("Тошо"), Some(130));
    |                            ^^^^^^^^^^^^^ method not found in `EventLog`
    |
   ::: tests/../src/lib.rs:10:1
    |
10  | struct EventLog{ events: Vec<Event>, }
    | --------------- method `filter_events` not found for this struct
For more information about this error, try `rustc --explain E0599`.
error: could not compile `solution` (test "solution_test") due to 6 previous errors
