Решение на упр.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() -> Self {
Self { 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| match e {
Event::Purchase { user: u, amount, .. } if u == user => Some(*amount), _ => 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 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 pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling solution v0.1.0 (/tmp/d20251030-1757769-1hdqt8u/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 {
| ^^^^^^^^
warning: associated items `new`, `add_event`, `user_spent`, and `summaries_by_type` are never used
--> src/lib.rs:16:8
|
14 | impl EventLog {
| ------------- associated items in this implementation
15 |
16 | fn new() -> Self {
| ^^^
...
20 | fn add_event(&mut self, event: Event) {
| ^^^^^^^^^
...
24 | fn user_spent(&self, user: &str) -> f64 {
| ^^^^^^^^^^
...
30 | 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 {
| --------------- 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 {
| --------------- 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 {
| --------------- 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 {
| --------------- 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 {
| --------------- 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 {
| --------------- 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
