Решение на упр.04 задача 1 от Константин Илиев
Към профила на Константин Илиев
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 2 неуспешни тест(а)
Код
enum Spec{
SI,
IS,
KN,
I,
M,
}
enum Title{
Assistant,
Doctor,
Professor,
}
struct Student{
name: String,
age: u32,
spec: Spec,
}
impl Student{
fn new(name: String, age: u32, spec: Spec) -> Self{
Self {name, age, spec}
}
}
struct Teacher{
name: String,
age: u32,
teach_spec: Vec<Spec>,
title: Title,
}
impl Teacher{
fn new(name: String, age: u32, teach_spec: Vec<Spec>, title: Title) -> Self{
Self {name, age, teach_spec, title}
}
}
struct University{
name: String,
list_of_stud: Vec<Student>,
list_of_teachers: Vec<Teacher>,
}
impl University{
fn new<T: IntoTeachers>(name: String, list_of_stud: Vec<Student>, teacher_or_stud: T) -> Self{
Self{
name,
list_of_stud,
list_of_teachers: teacher_or_stud.into_teachers(),
}
}
}
trait ToJson{
fn to_json(&self) -> String;
}
impl ToJson for Spec{
fn to_json(&self) -> String{
format!("\"{}\"", match self{
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
})
}
}
impl ToJson for Title{
fn to_json(&self) -> String{
format!("\"{}\"", match self{
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
})
}
}
impl ToJson for Student{
fn to_json(&self) -> String{
format!(r#"{{"name": {}, "age": {}, "spec": {}}}"#,
self.name,
self.age,
self.spec.to_json())
}
}
impl ToJson for Teacher{
fn to_json(&self) -> String{
let mut iter = self.teach_spec.iter();
let mut teach_spec_json = match iter.next(){
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter {
teach_spec_json.push_str(", ");
teach_spec_json.push_str(&e.to_json());
}
format!(r#"{{"name": {}, "age": {}, "spec": [{}], "title": {}}}"#,
self.name,
self.age,
teach_spec_json,
self.title.to_json())
}
}
impl ToJson for University{
fn to_json(&self) -> String{
let mut iter_list_of_stud = self.list_of_stud.iter();
let mut list_of_stud_json = match iter_list_of_stud.next(){
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter_list_of_stud {
list_of_stud_json.push_str(",\n ");
list_of_stud_json.push_str(&e.to_json());
}
let mut iter_list_of_teachers = self.list_of_teachers.iter();
let mut list_of_teachers_json = match iter_list_of_teachers.next(){
Some(first) => first.to_json(),
None => String::new(),
};
for i in iter_list_of_teachers {
list_of_teachers_json.push_str(", ");
list_of_teachers_json.push_str(&i.to_json());
}
format!(
r#"{{
"name": {},
"students": [
{}
],
"teachers": [
{}
]
}}"#,
self.name,
list_of_stud_json,
list_of_teachers_json)
}
}
trait IntoTeachers{
fn into_teachers(self) -> Vec<Teacher>;
}
impl IntoTeachers for Vec<Teacher>{
fn into_teachers(self) -> Vec<Teacher> {self}
}
impl IntoTeachers for Vec<Student>{
fn into_teachers(self) -> Vec<Teacher>{
let mut result = Vec::new();
for s in self{
let teacher = Teacher{
name: s.name,
age: s.age,
teach_spec: vec![s.spec],
title: Title::Assistant,
};
result.push(teacher);
}
result
}
}
Лог от изпълнението
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 slab v0.4.11
Compiling pin-utils v0.1.0
Compiling syn v2.0.109
Compiling pin-project-lite v0.2.16
Compiling futures-task v0.3.31
Compiling memchr v2.7.6
Compiling futures-io v0.3.31
Compiling solution v0.1.0 (/tmp/d20251106-1757769-39ceuj/solution)
warning: enum `Spec` is never used
--> src/lib.rs:1:6
|
1 | enum Spec{
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: enum `Title` is never used
--> src/lib.rs:9:6
|
9 | enum Title{
| ^^^^^
warning: struct `Student` is never constructed
--> src/lib.rs:15:8
|
15 | struct Student{
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:22:8
|
21 | impl Student{
| ------------ associated function in this implementation
22 | fn new(name: String, age: u32, spec: Spec) -> Self{
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:27:8
|
27 | struct Teacher{
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:35:8
|
34 | impl Teacher{
| ------------ associated function in this implementation
35 | fn new(name: String, age: u32, teach_spec: Vec<Spec>, title: Title) -> Self{
| ^^^
warning: struct `University` is never constructed
--> src/lib.rs:40:8
|
40 | struct University{
| ^^^^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:47:8
|
46 | impl University{
| --------------- associated function in this implementation
47 | fn new<T: IntoTeachers>(name: String, list_of_stud: Vec<Student>, teacher_or_stud: T) -> Self{
| ^^^
warning: trait `ToJson` is never used
--> src/lib.rs:56:7
|
56 | trait ToJson{
| ^^^^^^
warning: trait `IntoTeachers` is never used
--> src/lib.rs:155:7
|
155 | trait IntoTeachers{
| ^^^^^^^^^^^^
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:4:5
|
1 | enum Spec{
| ---- variants in this enum
...
4 | KN,
| ^^
5 | I,
| ^
6 | M,
| ^
|
= note: `#[warn(dead_code)]` on by default
warning: variant `Doctor` is never constructed
--> tests/../src/lib.rs:11:5
|
9 | enum Title{
| ----- variant in this enum
10 | Assistant,
11 | Doctor,
| ^^^^^^
warning: `solution` (test "solution_test") generated 2 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.86s
Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)
running 2 tests
test solution_test::test_basic2 ... FAILED
test solution_test::test_basic ... FAILED
failures:
---- solution_test::test_basic2 stdout ----
thread 'solution_test::test_basic2' panicked at tests/solution_test.rs:52:5:
assertion `left == right` failed
left: "{\"name\":SU,\"students\":[{\"name\":Ivan,\"age\":20,\"spec\":\"SI\"}],\"teachers\":[{\"name\":Peter,\"age\":25,\"spec\":[\"SI\"],\"title\":\"Assistant\"}]}"
right: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"spec\":\"SI\"}],\"teachers\":[{\"name\":\"Peter\",\"age\":25,\"spec\":\"SI\"}]}"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- solution_test::test_basic stdout ----
thread 'solution_test::test_basic' panicked at tests/solution_test.rs:32:5:
assertion `left == right` failed
left: "{\"name\":SU,\"students\":[{\"name\":Ivan,\"age\":20,\"spec\":\"SI\"},{\"name\":Peter,\"age\":25,\"spec\":\"SI\"}],\"teachers\":[{\"name\":Dr.Petrov,\"age\":45,\"spec\":[\"SI\",\"IS\"],\"title\":\"Professor\"}]}"
right: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"spec\":\"SI\"},{\"name\":\"Peter\",\"age\":25,\"spec\":\"SI\"}],\"teachers\":[{\"name\":\"Dr.Petrov\",\"age\":45,\"spec\":[\"SI\",\"IS\"],\"title\":\"Professor\"}]}"
failures:
solution_test::test_basic
solution_test::test_basic2
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--test solution_test`
