Решение на упр.04 задача 1 от Борил Василев
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
enum Spec
{
SI,
IS,
KN,
I,
M,
}
#[derive(Debug)]
enum Title
{
Assistant,
Doctor,
Professor,
}
#[derive(Debug)]
struct Student
{
name: String,
age: u8,
spec: Spec,
}
impl Student
{
fn new(name: String, age: u8, spec: Spec) -> Self
{
return Self
{
name,
age,
spec,
}
}
}
#[derive(Debug)]
struct Teacher
{
name: String,
age: u8,
specs: Vec<Spec>,
title: Title,
}
impl Teacher
{
fn new(name: String, age: u8, specs: Vec<Spec>, title: Title) -> Self
{
return Teacher
{
name,
age,
specs,
title,
}
}
}
#[derive(Debug)]
struct University<T>
{
name: String,
students: Vec<Student>,
teachers: Vec<T>,
}
impl<T> University<T>
{
fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self
{
return University
{
name,
students,
teachers,
}
}
}
trait ToJson
{
fn to_json(&self) -> String;
}
impl ToJson for Student
{
fn to_json(&self) -> String
{
return format!("{{\"name\":\"{}\",\"age\":{},\"spec\":\"{:?}\"}}",
self.name,
self.age,
self.spec);
}
}
impl ToJson for Teacher
{
fn to_json(&self) -> String
{
let specs_json = self.specs.iter()
.map(|e| format!("\"{:?}\"", e))
.collect::<Vec<String>>()
.join(",");
//trqbva da e specs, a ne spec ama w.e
return format!("{{\"name\":\"{}\",\"age\":{},\"spec\":[{}],\"title\":\"{:?}\"}}",
self.name,
self.age,
specs_json,
self.title);
}
}
impl<T> ToJson for University<T> where T: ToJson
{
fn to_json(&self) -> String
{
let students_json = self.students.iter()
.map(|e| e.to_json())
.collect::<Vec<String>>()
.join(",");
let teachers_json = self.teachers.iter()
.map(|e| e.to_json())
.collect::<Vec<String>>()
.join(",");
return format!("{{\"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 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 pin-utils v0.1.0
Compiling pin-project-lite v0.2.16
Compiling memchr v2.7.6
Compiling syn v2.0.109
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling futures-task v0.3.31
Compiling solution v0.1.0 (/tmp/d20251106-1757769-1hhwold/solution)
warning: enum `Spec` is never used
--> src/lib.rs:2:6
|
2 | enum Spec
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: enum `Title` is never used
--> src/lib.rs:12:6
|
12 | enum Title
| ^^^^^
warning: struct `Student` is never constructed
--> src/lib.rs:20:8
|
20 | struct Student
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:29:8
|
27 | impl Student
| ------------ associated function in this implementation
28 | {
29 | fn new(name: String, age: u8, spec: Spec) -> Self
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:41:8
|
41 | struct Teacher
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:51:8
|
49 | impl Teacher
| ------------ associated function in this implementation
50 | {
51 | fn new(name: String, age: u8, specs: Vec<Spec>, title: Title) -> Self
| ^^^
warning: struct `University` is never constructed
--> src/lib.rs:64:8
|
64 | struct University<T>
| ^^^^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:73:8
|
71 | impl<T> University<T>
| --------------------- associated function in this implementation
72 | {
73 | fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self
| ^^^
warning: trait `ToJson` is never used
--> src/lib.rs:84:7
|
84 | trait ToJson
| ^^^^^^
warning: `solution` (lib) generated 9 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: variants `KN`, `I`, and `M` are never constructed
--> tests/../src/lib.rs:6:5
|
2 | enum Spec
| ---- variants in this enum
...
6 | KN,
| ^^
7 | I,
| ^
8 | M,
| ^
|
= note: `Spec` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: variants `Assistant` and `Doctor` are never constructed
--> tests/../src/lib.rs:14:5
|
12 | enum Title
| ----- variants in this enum
13 | {
14 | Assistant,
| ^^^^^^^^^
15 | Doctor,
| ^^^^^^
|
= note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
warning: `solution` (test "solution_test") generated 2 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.55s
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
