Решение на упр.04 задача 1 от Нели Илиева
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug, Clone)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
#[derive(Debug, Clone)]
enum Title {
Assistant,
Doctor,
Professor,
}
trait ToJson: std::fmt::Debug {
fn to_json(&self) -> String;
}
impl ToJson for Spec {
fn to_json(&self) -> String {
match self {
Spec::SI => "\"SI\"".to_string(),
Spec::IS => "\"IS\"".to_string(),
Spec::KN => "\"KN\"".to_string(),
Spec::I => "\"I\"".to_string(),
Spec::M => "\"M\"".to_string(),
}
}
}
impl ToJson for Title {
fn to_json(&self) -> String {
match self {
Title::Assistant => "\"Assistant\"".to_string(),
Title::Doctor => "\"Doctor\"".to_string(),
Title::Professor => "\"Professor\"".to_string(),
}
}
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Student { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format! (
r#"{{
"name": "{}",
"age": {},
"spec": {}
}}"#,
self.name,
self.age,
self.spec.to_json()
)
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u32,
spec: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, spec: Vec<Spec>, title: Title) -> Self {
Teacher { name, age, spec, title }
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let mut iter = self.spec.iter();
let mut result = match iter.next() {
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter {
result.push_str(", ");
result.push_str(&e.to_json());
}
format!(
r#"{{"name": "{}", "age": {}, "spec": [{}], "title": {}}}"#,
self.name, self.age, result, self.title.to_json()
)
}
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Box<dyn ToJson>>,
teachers: Vec<Box<dyn ToJson>>,
}
impl University {
fn new<S: ToJson + 'static, T: ToJson + 'static>(
name: String,
students: Vec<S>,
teachers: Vec<T>,
) -> Self {
let mut s_vec: Vec<Box<dyn ToJson>> = Vec::new();
for s in students {
s_vec.push(Box::new(s));
}
let mut t_vec: Vec<Box<dyn ToJson>> = Vec::new();
for t in teachers {
t_vec.push(Box::new(t));
}
University {
name,
students: s_vec,
teachers: t_vec,
}
}
}
impl ToJson for University {
fn to_json(&self) -> String {
let mut iter_s = self.students.iter();
let mut iter_t = self.teachers.iter();
let mut res_s = match iter_s.next() {
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter_s {
res_s.push_str(", ");
res_s.push_str(&e.to_json());
}
let mut res_t = match iter_t.next() {
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter_t {
res_t.push_str(", ");
res_t.push_str(&e.to_json());
}
format!(
r#"{{"name": "{}", "students": [{}], "teachers": [{}]}}"#,
self.name, res_s, res_t
)
}
}
fn main() {
let s1 = Student::new("Ivan".to_string(), 20, Spec::SI);
let s2 = Student::new("Peter".to_string(), 25, Spec::SI);
let t1 = Teacher::new(
"Dr. Petrov".to_string(),
45,
vec![Spec::SI, Spec::IS],
Title::Professor,
);
let uni = University::new("SU".to_string(), vec![s1, s2], vec![t1]);
println!("{}", uni.to_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-core v0.3.31
Compiling futures-sink v0.3.31
Compiling futures-channel v0.3.31
Compiling pin-project-lite v0.2.16
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-utils v0.1.0
Compiling solution v0.1.0 (/tmp/d20251106-1757769-nlwokm/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:11:6
|
11 | enum Title {
| ^^^^^
warning: trait `ToJson` is never used
--> src/lib.rs:17:7
|
17 | trait ToJson: std::fmt::Debug {
| ^^^^^^
warning: struct `Student` is never constructed
--> src/lib.rs:44:8
|
44 | struct Student {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:51:8
|
50 | impl Student {
| ------------ associated function in this implementation
51 | fn new(name: String, age: u32, spec: Spec) -> Self {
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:72:8
|
72 | struct Teacher {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:80:8
|
79 | impl Teacher {
| ------------ associated function in this implementation
80 | fn new(name: String, age: u32, spec: Vec<Spec>, title: Title) -> Self {
| ^^^
warning: struct `University` is never constructed
--> src/lib.rs:107:8
|
107 | struct University {
| ^^^^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:114:8
|
113 | impl University {
| --------------- associated function in this implementation
114 | fn new<S: ToJson + 'static, T: ToJson + 'static>(
| ^^^
warning: function `main` is never used
--> src/lib.rs:169:4
|
169 | fn main() {
| ^^^^
warning: `solution` (lib) generated 10 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 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:12:5
|
11 | enum Title {
| ----- variants in this enum
12 | Assistant,
| ^^^^^^^^^
13 | Doctor,
| ^^^^^^
|
= note: `Title` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
warning: function `main` is never used
--> tests/../src/lib.rs:169:4
|
169 | fn main() {
| ^^^^
warning: `solution` (test "solution_test") generated 3 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.18s
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
