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

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

Към профила на Виктор Карталов

Резултати

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

Код

use std::fmt::{self, Debug};
#[derive(Debug, Clone)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
#[derive(Debug, Clone)]
enum Title {
Assistant,
Doctor,
Professor,
}
trait ToJson {
fn to_json(&self) -> String;
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Student { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\": \"{}\", \"age\": {}, \"spec\": \"{:?}\"}}",
self.name, self.age, self.spec
)
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u32,
specs: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Teacher {
name,
age,
specs,
title,
}
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json: Vec<String> = self.specs.iter()
.map(|s| format!("\"{:?}\"", s))
.collect();
format!(
"{{\"name\": \"{}\", \"age\": {}, \"spec\": [{}], \"title\": \"{:?}\"}}",
self.name,
self.age,
specs_json.join(", "),
self.title
)
}
}
#[derive(Debug)]
struct University<TeacherType> {
name: String,
students: Vec<Student>,
teachers: Vec<TeacherType>,
}
impl<TeacherType: ToJson + Debug> 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 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 futures-task v0.3.31
   Compiling syn v2.0.109
   Compiling futures-io v0.3.31
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1f4bnv6/solution)
warning: unused import: `self`
 --> src/lib.rs:1:16
  |
1 | use std::fmt::{self, Debug};
  |                ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

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:13:6
   |
13 | enum Title {
   |      ^^^^^

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

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

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

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

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

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

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

warning: `solution` (lib) generated 10 warnings (run `cargo fix --lib -p solution` to apply 1 suggestion)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: unused import: `self`
 --> tests/../src/lib.rs:1:16
  |
1 | use std::fmt::{self, Debug};
  |                ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

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:14:5
   |
13 | enum Title {
   |      ----- variants in this enum
14 |     Assistant,
   |     ^^^^^^^^^
15 |     Doctor,
   |     ^^^^^^
   |
   = 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 3 warnings (run `cargo fix --test "solution_test"` to apply 1 suggestion)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.21s
     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 22:01 (преди около 1 месеца)