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

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

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

Резултати

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

Код

use std::fmt::Debug;
trait ToJson
where
Self: Debug,
{
fn to_json(&self) -> String {
format!("\"{:?}\"", self)
}
}
impl<T: ToJson> ToJson for Vec<T> {
fn to_json(&self) -> String {
format!(
"[{}]",
self.iter()
.map(T::to_json)
.collect::<Vec<String>>()
.join(", ")
)
}
}
#[derive(Debug)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToJson for Spec {}
#[derive(Debug)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl ToJson for Title {}
#[derive(Debug)]
struct Student {
name: String,
age: i32,
spec: Spec,
}
impl Student {
fn new(name: String, age: i32, spec: Spec) -> Self {
Student {
name: name,
age,
spec,
}
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{ \"name\": \"{}\", \"age\": {}, \"spec\": {}}}",
self.name,
self.age,
self.spec.to_json()
)
}
}
#[derive(Debug)]
struct Teacher {
name: String,
age: i32,
spec: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: i32, spec: Vec<Spec>, title: Title) -> Self {
Teacher {
name,
age,
spec,
title,
}
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
format!(
"{{ \"name\": \"{}\", \"age\": {}, \"spec\": {}, \"title\": {}}}",
self.name,
self.age,
self.spec.to_json(),
self.title.to_json()
)
}
}
#[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,
}
}
}
impl<T: ToJson> ToJson for University<T> {
fn to_json(&self) -> String {
format!(
"{{ \"name\": \"{}\", \"students\": {}, \"teachers\": {}}}",
self.name,
self.students.to_json(),
self.teachers.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-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.16
   Compiling futures-io v0.3.31
   Compiling syn v2.0.109
   Compiling futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-rqbwen/solution)
warning: trait `ToJson` is never used
 --> src/lib.rs:3:7
  |
3 | trait ToJson
  |       ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

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

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

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

warning: associated function `new` is never used
  --> src/lib.rs:52:8
   |
51 | impl Student {
   | ------------ associated function in this implementation
52 |     fn new(name: String, age: i32, spec: Spec) -> Self {
   |        ^^^

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

warning: associated function `new` is never used
  --> src/lib.rs:81:8
   |
80 | impl Teacher {
   | ------------ associated function in this implementation
81 |     fn new(name: String, age: i32, spec: Vec<Spec>, title: Title) -> Self {
   |        ^^^

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

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

warning: `solution` (lib) generated 9 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:28:5
   |
25 | enum Spec {
   |      ---- variants in this enum
...
28 |     KN,
   |     ^^
29 |     I,
   |     ^
30 |     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:37:5
   |
36 | enum Title {
   |      ----- variants in this enum
37 |     Assistant,
   |     ^^^^^^^^^
38 |     Doctor,
   |     ^^^^^^
   |
   = note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: `solution` (test "solution_test") generated 2 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.26s
     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 коментара)

Деян качи първо решение на 03.11.2025 14:51 (преди около 1 месеца)