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

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

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

Резултати

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

Код

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

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

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

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

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

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

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

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

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

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:9:5
   |
6  | enum Spec {
   |      ---- variants in this enum
...
9  |     KN,
   |     ^^
10 |     I,
   |     ^
11 |     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:16:5
   |
15 | enum Title {
   |      ----- variants in this enum
16 |     Assistant,
   |     ^^^^^^^^^
17 |     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.58s
     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 коментара)

Димитър качи първо решение на 31.10.2025 15:06 (преди около 1 месеца)