Решение на упр.04 задача 1 от Йоанна Ненкова
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl Spec {
fn to_string(&self) -> &str {
match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
}
}
}
#[derive(Debug)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl Title {
fn to_string(&self) -> &str {
match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
}
}
}
#[derive(Debug)]
struct Student {
name: String,
age: u8,
spec: Spec,
}
impl Student {
fn new(name: String, age: u8, spec: Spec) -> Self {
Student {name, age, spec}
}
}
#[derive(Debug)]
struct Teacher {
name: String,
age: u8,
spec: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u8, spec: Vec<Spec>, title: Title) -> Self {
Teacher {name, age, spec, title}
}
}
#[derive(Debug)]
struct University<T: ToJson> {
name: String,
students: Vec<Student>,
teachers: Vec<T>,
}
impl<T: ToJson> University<T> {
fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
University {name, students, teachers}
}
}
trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for Spec {
fn to_json(&self) -> String {
format!("\"{}\"", self.to_string())
}
}
impl ToJson for Title {
fn to_json(&self) -> String {
format!("\"{}\"", self.to_string())
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\": \"{}\", \"age\": {}, \"spec\": {}}}",
self.name,
self.age,
self.spec.to_json()
)
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let mut specs_json = String::new();
for (i, s) in self.spec.iter().enumerate() {
if i > 0 {
specs_json.push_str(", ");
}
specs_json.push_str(&format!("{}", s.to_json()))
}
format!(
"{{\"name\": \"{}\", \"age\": {}, \"spec\": [{}], \"title\": {}}}",
self.name,
self.age,
specs_json,
self.title.to_json()
)
}
}
impl<T: ToJson> ToJson for University<T> {
fn to_json(&self) -> String {
let mut students_json = String::new();
for (i, s) in self.students.iter().enumerate() {
if i > 0 {
students_json.push_str(", ");
}
students_json.push_str(&format!("{}", s.to_json()))
}
let mut teachers_json = String::new();
for (i, t) in self.teachers.iter().enumerate() {
if i > 0 {
teachers_json.push_str(", ");
}
teachers_json.push_str(&format!("{}", t.to_json()))
}
format!(
"{{\"name\": \"{}\", \"students\": [{}], \"teachers\": [{}]}}",
self.name,
students_json,
teachers_json
)
}
}
fn main() {
let s1 = Student::new("Ivan".to_string(), 23, Spec::SI);
let t1 = Teacher::new("Peter".to_string(), 45, vec![Spec::SI, Spec::IS], Title::Professor);
let uni = University::new("SU".to_string(), vec![s1], vec![t1]);
println!("{}", uni.to_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-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling memchr v2.7.6
Compiling syn v2.0.109
Compiling futures-task v0.3.31
Compiling slab v0.4.11
Compiling futures-io v0.3.31
Compiling pin-utils v0.1.0
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251106-1757769-55xugg/solution)
warning: enum `Spec` is never used
--> src/lib.rs:2:6
|
2 | enum Spec {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: method `to_string` is never used
--> src/lib.rs:11:8
|
10 | impl Spec {
| --------- method in this implementation
11 | fn to_string(&self) -> &str {
| ^^^^^^^^^
warning: enum `Title` is never used
--> src/lib.rs:23:6
|
23 | enum Title {
| ^^^^^
warning: method `to_string` is never used
--> src/lib.rs:30:8
|
29 | impl Title {
| ---------- method in this implementation
30 | fn to_string(&self) -> &str {
| ^^^^^^^^^
warning: struct `Student` is never constructed
--> src/lib.rs:40:8
|
40 | struct Student {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:47:8
|
46 | impl Student {
| ------------ associated function in this implementation
47 | fn new(name: String, age: u8, spec: Spec) -> Self {
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:53:8
|
53 | struct Teacher {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:61:8
|
60 | impl Teacher {
| ------------ associated function in this implementation
61 | fn new(name: String, age: u8, spec: Vec<Spec>, title: Title) -> Self {
| ^^^
warning: struct `University` is never constructed
--> src/lib.rs:67:8
|
67 | struct University<T: ToJson> {
| ^^^^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:74:8
|
73 | impl<T: ToJson> University<T> {
| ----------------------------- associated function in this implementation
74 | fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
| ^^^
warning: trait `ToJson` is never used
--> src/lib.rs:79:7
|
79 | trait ToJson {
| ^^^^^^
warning: function `main` is never used
--> src/lib.rs:153:4
|
153 | fn main() {
| ^^^^
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:5:5
|
2 | enum Spec {
| ---- variants in this enum
...
5 | KN,
| ^^
6 | I,
| ^
7 | 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:24:5
|
23 | enum Title {
| ----- variants in this enum
24 | Assistant,
| ^^^^^^^^^
25 | Doctor,
| ^^^^^^
|
= note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
warning: function `main` is never used
--> tests/../src/lib.rs:153:4
|
153 | fn main() {
| ^^^^
warning: `solution` (test "solution_test") generated 3 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.34s
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
