Решение на упр.04 задача 1 от Пламен Колев
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum Spec {
SI,
IS,
KN,
I,
M
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum Title {
Assistant,
Doctor,
Professor
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u8,
spec: Spec
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u8,
specs: Vec<Spec>,
title: Title
}
#[derive(Debug, Clone)]
struct University<S, T> {
name: String,
students: Vec<S>,
teachers: Vec<T>,
}
trait ToJson {
fn to_json(&self) -> String;
}
impl Student {
fn new(name: String, age: u8, spec: Spec) -> Student {
Student { name, age, spec }
}
}
impl Teacher {
fn new(name: String, age: u8, specs: Vec<Spec>, title: Title) -> Teacher {
Teacher { name, age, specs, title }
}
}
impl<S, T> University<S, T> {
fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> University<S, T> {
University { name, students, teachers }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
r#"{{ "name": "{}", "age": {}, "spec": "{}" }}"#,
self.name,
self.age,
match self.spec {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
}
)
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json = self.specs
.iter()
.map(|s| format!("\"{}\"",
match s {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
}
))
.collect::<Vec<_>>()
.join(",");
format!(
r#"{{"name":"{}","age":{},"spec":[{}],"title":"{}"}}"#,
self.name,
self.age,
specs_json,
match self.title {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
}
)
}
}
impl<S, T> ToJson for University<S, T>
where
S: ToJson,
T: ToJson,
{
fn to_json(&self) -> String {
let students_json = self.students
.iter()
.map(|s| s.to_json())
.collect::<Vec<_>>()
.join(",");
let teachers_json = self.teachers
.iter()
.map(|t| t.to_json())
.collect::<Vec<_>>()
.join(",");
format!(
r#"{{
"name": "{}",
"students": [{}],
"teachers": [{}]
}}"#,
self.name,
students_json,
teachers_json
)
}
}
Лог от изпълнението
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 pin-utils v0.1.0
Compiling pin-project-lite v0.2.16
Compiling memchr v2.7.6
Compiling syn v2.0.109
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling solution v0.1.0 (/tmp/d20251106-1757769-fnf1wu/solution)
warning: struct `Student` is never constructed
--> src/lib.rs:20:8
|
20 | struct Student {
| ^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: struct `Teacher` is never constructed
--> src/lib.rs:27:8
|
27 | struct Teacher {
| ^^^^^^^
warning: struct `University` is never constructed
--> src/lib.rs:35:8
|
35 | struct University<S, T> {
| ^^^^^^^^^^
warning: trait `ToJson` is never used
--> src/lib.rs:41:7
|
41 | trait ToJson {
| ^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:46:8
|
45 | impl Student {
| ------------ associated function in this implementation
46 | fn new(name: String, age: u8, spec: Spec) -> Student {
| ^^^
warning: associated function `new` is never used
--> src/lib.rs:52:8
|
51 | impl Teacher {
| ------------ associated function in this implementation
52 | fn new(name: String, age: u8, specs: Vec<Spec>, title: Title) -> Teacher {
| ^^^
warning: associated function `new` is never used
--> src/lib.rs:58:8
|
57 | impl<S, T> University<S, T> {
| --------------------------- associated function in this implementation
58 | fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> University<S, T> {
| ^^^
warning: `solution` (lib) generated 7 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.26s
Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)
running 2 tests
test solution_test::test_basic2 ... 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
