Решение на упр.04 задача 1 от Станислав Стаматов
Към профила на Станислав Стаматов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::fmt::Display;
#[derive(Debug)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl Display for Spec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Spec::SI => write!(f, "SI"),
Spec::IS => write!(f, "IS"),
Spec::KN => write!(f, "KN"),
Spec::I => write!(f, "I"),
Spec::M => write!(f, "M"),
}
}
}
#[derive(Debug)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl Display for Title {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Title::Assistant => write!(f, "Assistant"),
Title::Doctor => write!(f, "Doctor"),
Title::Professor => write!(f, "Professor"),
}
}
}
///////////////////////////////////////////////////////////////
trait UniversityMember {}
trait ToJson {
fn to_json(&self) -> String;
}
///////////////////////////////////////////////////////////////
#[derive(Debug)]
struct Person {
name: String,
age: i32,
}
impl Person {
pub fn new(name: String, age: i32) -> Self {
Self { name, age }
}
}
impl ToJson for Person {
fn to_json(&self) -> String {
format!("\"name\": \"{}\", \"age\": {}", self.name, self.age)
}
}
///////////////////////////////////////////////////////////////
#[derive(Debug)]
struct Student {
person_info: Person,
spec: Spec,
}
impl Student {
pub fn new(name: String, age: i32, spec: Spec) -> Self {
Self {
person_info: Person::new(name, age),
spec: spec,
}
}
}
impl UniversityMember for Student {}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{}, \"spec\": \"{}\"",
self.person_info.to_json(),
self.spec
)
}
}
///////////////////////////////////////////////////////////////
#[derive(Debug)]
struct Teacher {
person_info: Person,
teaching_spec: Vec<Spec>,
title: Title,
}
impl Teacher {
pub fn new(name: String, age: i32, teaching_spec: Vec<Spec>, title: Title) -> Self {
Self {
person_info: Person::new(name, age),
teaching_spec: teaching_spec,
title: title,
}
}
fn teaching_spec_as_String(&self) -> String {
self.teaching_spec
.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<String>>()
.join(", ")
}
}
impl UniversityMember for Teacher {}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let teach_spec = format!("[{}]", self.teaching_spec_as_String());
let result = format!(
"{}, \"spec\": {}, \"title\": \"{}\"",
self.person_info.to_json(),
teach_spec,
self.title
);
result
}
}
///////////////////////////////////////////////////////////////
#[derive(Debug)]
struct University<T>
where
T: UniversityMember + ToJson,
{
name: String,
students: Vec<Student>,
teachers: Vec<T>,
}
impl<T> University<T>
where
T: UniversityMember + ToJson,
{
pub fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
Self {
name: name,
students: students,
teachers: teachers,
}
}
pub fn students_as_String(&self) -> String {
self.students
.iter()
.map(|s| format!("{{{}}}", s.to_json()))
.collect::<Vec<String>>()
.join(",\n")
}
pub fn teachers_as_String(&self) -> String {
self.teachers
.iter()
.map(|s| format!("{{{}}}", s.to_json()))
.collect::<Vec<String>>()
.join(",\n")
}
}
impl<T> ToJson for University<T>
where
T: UniversityMember + ToJson,
{
fn to_json(&self) -> String {
let mut result = format!("{{\"name\":\"{}\",\n", self.name);
result += &format!("\"students\": [\n{} \n],\n", self.students_as_String());
result += &format!("\"teachers\": [\n{} \n]\n", self.teachers_as_String());
result += "}";
result
}
}
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 slab v0.4.11
Compiling syn v2.0.109
Compiling futures-io v0.3.31
Compiling futures-task v0.3.31
Compiling memchr v2.7.6
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling solution v0.1.0 (/tmp/d20251106-1757769-pxdct7/solution)
warning: enum `Spec` is never used
--> src/lib.rs:4:6
|
4 | enum Spec {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: enum `Title` is never used
--> src/lib.rs:24:6
|
24 | enum Title {
| ^^^^^
warning: trait `UniversityMember` is never used
--> src/lib.rs:40:7
|
40 | trait UniversityMember {}
| ^^^^^^^^^^^^^^^^
warning: trait `ToJson` is never used
--> src/lib.rs:41:7
|
41 | trait ToJson {
| ^^^^^^
warning: struct `Person` is never constructed
--> src/lib.rs:48:8
|
48 | struct Person {
| ^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:53:12
|
52 | impl Person {
| ----------- associated function in this implementation
53 | pub fn new(name: String, age: i32) -> Self {
| ^^^
warning: struct `Student` is never constructed
--> src/lib.rs:66:8
|
66 | struct Student {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:71:12
|
70 | impl Student {
| ------------ associated function in this implementation
71 | pub fn new(name: String, age: i32, spec: Spec) -> Self {
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:91:8
|
91 | struct Teacher {
| ^^^^^^^
warning: associated items `new` and `teaching_spec_as_String` are never used
--> src/lib.rs:97:12
|
96 | impl Teacher {
| ------------ associated items in this implementation
97 | pub fn new(name: String, age: i32, teaching_spec: Vec<Spec>, title: Title) -> Self {
| ^^^
...
104 | fn teaching_spec_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^
warning: struct `University` is never constructed
--> src/lib.rs:130:8
|
130 | struct University<T>
| ^^^^^^^^^^
warning: associated items `new`, `students_as_String`, and `teachers_as_String` are never used
--> src/lib.rs:142:12
|
138 | / impl<T> University<T>
139 | | where
140 | | T: UniversityMember + ToJson,
| |_________________________________- associated items in this implementation
141 | {
142 | pub fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
| ^^^
...
149 | pub fn students_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^
...
156 | pub fn teachers_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^
warning: function `main` is never used
--> src/lib.rs:179:4
|
179 | fn main() {
| ^^^^
warning: method `teaching_spec_as_String` should have a snake case name
--> src/lib.rs:104:8
|
104 | fn teaching_spec_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `teaching_spec_as_string`
|
= note: `#[warn(non_snake_case)]` on by default
warning: method `students_as_String` should have a snake case name
--> src/lib.rs:149:12
|
149 | pub fn students_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `students_as_string`
warning: method `teachers_as_String` should have a snake case name
--> src/lib.rs:156:12
|
156 | pub fn teachers_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `teachers_as_string`
warning: `solution` (lib) generated 16 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 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:25:5
|
24 | enum Title {
| ----- variants in this enum
25 | Assistant,
| ^^^^^^^^^
26 | 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:179:4
|
179 | fn main() {
| ^^^^
warning: method `teaching_spec_as_String` should have a snake case name
--> tests/../src/lib.rs:104:8
|
104 | fn teaching_spec_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `teaching_spec_as_string`
|
= note: `#[warn(non_snake_case)]` on by default
warning: method `students_as_String` should have a snake case name
--> tests/../src/lib.rs:149:12
|
149 | pub fn students_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `students_as_string`
warning: method `teachers_as_String` should have a snake case name
--> tests/../src/lib.rs:156:12
|
156 | pub fn teachers_as_String(&self) -> String {
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `teachers_as_string`
warning: `solution` (test "solution_test") generated 6 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.29s
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
