Решение на упр.04 задача 1 от Калоян Стоянов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::fmt;
#[derive(Debug, Clone)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl fmt::Display for Spec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
};
write!(f, "{}", s)
}
}
#[derive(Debug, Clone)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl fmt::Display for Title {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
};
write!(f, "{}", s)
}
}
fn escape_json(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
trait ToJson {
fn to_json(&self) -> String;
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":\"{}\"}}",
escape_json(&self.name),
self.age,
self.spec
)
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u32,
specs: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Self {
name,
age,
specs,
title,
}
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json: String = self
.specs
.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<_>>()
.join(",");
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\": [{}],\"title\":\"{}\"}}",
escape_json(&self.name),
self.age,
specs_json,
self.title
)
}
}
#[derive(Debug, Clone)]
enum Person {
Student(Student),
Teacher(Teacher),
}
impl From<Student> for Person {
fn from(s: Student) -> Self {
Person::Student(s)
}
}
impl From<Teacher> for Person {
fn from(t: Teacher) -> Self {
Person::Teacher(t)
}
}
impl ToJson for Person {
fn to_json(&self) -> String {
match self {
Person::Student(s) => s.to_json(),
Person::Teacher(t) => t.to_json(),
}
}
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Student>,
teachers: Vec<Person>,
}
impl University {
fn new<T>(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self
where
T: Into<Person>,
{
let teachers = teachers.into_iter().map(|t| t.into()).collect();
Self {
name,
students,
teachers,
}
}
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!(
"{{\"name\":\"{}\",\"students\":[{}],\"teachers\":[{}]}}",
escape_json(&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 futures-io v0.3.31
Compiling syn v2.0.109
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling futures-task v0.3.31
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251106-1757769-7ip2o1/solution)
warning: variants `SI`, `IS`, `KN`, `I`, and `M` are never constructed
--> src/lib.rs:5:5
|
4 | enum Spec {
| ---- variants in this enum
5 | SI,
| ^^
6 | IS,
| ^^
7 | KN,
| ^^
8 | I,
| ^
9 | M,
| ^
|
= note: `Spec` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: variants `Assistant`, `Doctor`, and `Professor` are never constructed
--> src/lib.rs:27:5
|
26 | enum Title {
| ----- variants in this enum
27 | Assistant,
| ^^^^^^^^^
28 | Doctor,
| ^^^^^^
29 | Professor,
| ^^^^^^^^^
|
= note: `Title` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
warning: function `escape_json` is never used
--> src/lib.rs:43:4
|
43 | fn escape_json(s: &str) -> String {
| ^^^^^^^^^^^
warning: trait `ToJson` is never used
--> src/lib.rs:47:7
|
47 | trait ToJson {
| ^^^^^^
warning: fields `name`, `age`, and `spec` are never read
--> src/lib.rs:53:5
|
52 | struct Student {
| ------- fields in this struct
53 | name: String,
| ^^^^
54 | age: u32,
| ^^^
55 | spec: Spec,
| ^^^^
|
= note: `Student` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
warning: associated function `new` is never used
--> src/lib.rs:59:8
|
58 | impl Student {
| ------------ associated function in this implementation
59 | fn new(name: String, age: u32, spec: Spec) -> Self {
| ^^^
warning: fields `name`, `age`, `specs`, and `title` are never read
--> src/lib.rs:77:5
|
76 | struct Teacher {
| ------- fields in this struct
77 | name: String,
| ^^^^
78 | age: u32,
| ^^^
79 | specs: Vec<Spec>,
| ^^^^^
80 | title: Title,
| ^^^^^
|
= note: `Teacher` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
warning: associated function `new` is never used
--> src/lib.rs:84:8
|
83 | impl Teacher {
| ------------ associated function in this implementation
84 | fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
| ^^^
warning: field `0` is never read
--> src/lib.rs:115:13
|
115 | Student(Student),
| ------- ^^^^^^^
| |
| field in this variant
|
= note: `Person` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
|
115 | Student(()),
| ~~
warning: field `0` is never read
--> src/lib.rs:116:13
|
116 | Teacher(Teacher),
| ------- ^^^^^^^
| |
| field in this variant
|
= note: `Person` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
|
116 | Teacher(()),
| ~~
warning: struct `University` is never constructed
--> src/lib.rs:141:8
|
141 | struct University {
| ^^^^^^^^^^
warning: associated items `new` and `to_json` are never used
--> src/lib.rs:148:8
|
147 | impl University {
| --------------- associated items in this implementation
148 | fn new<T>(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self
| ^^^
...
160 | fn to_json(&self) -> String {
| ^^^^^^^
warning: `solution` (lib) generated 12 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:7:5
|
4 | enum Spec {
| ---- variants in this enum
...
7 | KN,
| ^^
8 | I,
| ^
9 | M,
| ^
|
= note: `Spec` has derived impls for the traits `Clone` and `Debug`, but these are 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:27:5
|
26 | enum Title {
| ----- variants in this enum
27 | Assistant,
| ^^^^^^^^^
28 | Doctor,
| ^^^^^^
|
= note: `Title` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
warning: `solution` (test "solution_test") generated 2 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.54s
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
