Решение на упр.04 задача 1 от Илиян Копривчин

Обратно към всички решения

Към профила на Илиян Копривчин

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 2 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::fmt::Debug;
#[derive(Debug, Clone)]
enum Spec {
SI,
IS,
KN,
I,
M
}
impl Spec {
fn to_str(&self) -> &str {
match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
}
}
}
#[derive(Debug, Clone)]
enum Title {
Assistant, Doctor, Professor
}
impl Title {
fn to_str(&self) -> &str {
match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
}
}
}
trait ToJson {
fn to_json(&self) -> String;
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u32,
specialty: Spec,
}
impl Student {
fn new(name: String, age: u32, specialty: Spec) -> Self {
Student { name, age, specialty }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":\"{}\"}}",
self.name,
self.age,
self.specialty.to_str()
)
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u32,
teaching_specialties: Vec<Spec>,
academic_title: Title,
}
impl Teacher {
fn new(name: String, age: u32, teaching_specialties: Vec<Spec>, academic_title: Title) -> Self {
Teacher {
name,
age,
teaching_specialties,
academic_title,
}
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json: Vec<String> = self.teaching_specialties.iter().map(|s| format!("\"{}\"", s.to_str())).collect();
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":[{}],\"title\":\"{}\"}}",
self.name,
self.age,
specs_json.join(","),
self.academic_title.to_str()
)
}
}
#[derive(Debug)]
struct University<TeacherType> {
name: String,
students: Vec<Student>,
teachers: Vec<TeacherType>,
}
impl<TeacherType> University<TeacherType> {
fn new(name: String, students: Vec<Student>, teachers: Vec<TeacherType>) -> Self {
University {
name,
students,
teachers,
}
}
}
impl<TeacherType: ToJson + Debug> ToJson for University<TeacherType> {
fn to_json(&self) -> String {
let students_json: Vec<String> = self.students.iter().map(|s| s.to_json()).collect();
let teachers_json: Vec<String> = self.teachers.iter().map(|t| t.to_json()).collect();
format!(
"{{\"name\":\"{}\",\"students\":[{}],\"teachers\":[{}]}}",
self.name,
students_json.join(","),
teachers_json.join(",")
)
}
}

Лог от изпълнението

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-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling syn v2.0.109
   Compiling futures-io v0.3.31
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1iwo1l7/solution)
warning: enum `Spec` is never used
 --> src/lib.rs:4:6
  |
4 | enum Spec {
  |      ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `to_str` is never used
  --> src/lib.rs:13:8
   |
12 | impl Spec {
   | --------- method in this implementation
13 |     fn to_str(&self) -> &str {
   |        ^^^^^^

warning: enum `Title` is never used
  --> src/lib.rs:25:6
   |
25 | enum Title {
   |      ^^^^^

warning: method `to_str` is never used
  --> src/lib.rs:30:8
   |
29 | impl Title {
   | ---------- method in this implementation
30 |     fn to_str(&self) -> &str {
   |        ^^^^^^

warning: trait `ToJson` is never used
  --> src/lib.rs:39:7
   |
39 | trait ToJson {
   |       ^^^^^^

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, specialty: Spec) -> Self {
   |        ^^^

warning: struct `Teacher` is never constructed
  --> src/lib.rs:68:8
   |
68 | struct Teacher {
   |        ^^^^^^^

warning: associated function `new` is never used
  --> src/lib.rs:76:8
   |
75 | impl Teacher {
   | ------------ associated function in this implementation
76 |     fn new(name: String, age: u32, teaching_specialties: Vec<Spec>, academic_title: Title) -> Self {
   |        ^^^

warning: struct `University` is never constructed
   --> src/lib.rs:100:8
    |
100 | struct University<TeacherType> {
    |        ^^^^^^^^^^

warning: associated function `new` is never used
   --> src/lib.rs:107:8
    |
106 | impl<TeacherType> University<TeacherType> {
    | ----------------------------------------- associated function in this implementation
107 |     fn new(name: String, students: Vec<Student>, teachers: Vec<TeacherType>) -> Self {
    |        ^^^

warning: `solution` (lib) generated 11 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:26:5
   |
25 | enum Title {
   |      ----- variants in this enum
26 |     Assistant, Doctor, Professor
   |     ^^^^^^^^^  ^^^^^^
   |
   = 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.17s
     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

История (1 версия и 0 коментара)

Илиян качи първо решение на 05.11.2025 23:10 (преди около 1 месеца)