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

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

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

Резултати

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

Код

use std::fmt::Debug;
#[derive(Debug, Clone)]
enum Spec {
SI, IS, KN, I, M,
}
#[derive(Debug, Clone)]
enum Title {
Assistant, Doctor, Professor,
}
#[derive(Debug, Clone)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: u32,
spec: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, spec: Vec<Spec>, title: Title) -> Self {
Self { name, age, spec, title }
}
}
#[derive(Debug, Clone)]
enum TeacherRole {
Professor(Teacher),
StudentTeacher(Student),
}
trait IntoTeacherRole {
fn into_role(self) -> TeacherRole;
}
impl IntoTeacherRole for Teacher {
fn into_role(self) -> TeacherRole {
TeacherRole::Professor(self)
}
}
impl IntoTeacherRole for Student {
fn into_role(self) -> TeacherRole {
TeacherRole::StudentTeacher(self)
}
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Student>,
teachers: Vec<TeacherRole>,
}
impl University {
fn new<T>(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self
where
T: IntoTeacherRole,
{
University {
name,
students,
teachers: teachers.into_iter().map(|t| t.into_role()).collect(),
}
}
}
trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for Spec {
fn to_json(&self) -> String {
format!("\"{:?}\"", self)
}
}
impl ToJson for Title {
fn to_json(&self) -> String {
format!("\"{:?}\"", self)
}
}
impl<T> ToJson for Vec<T>
where
T: ToJson,
{
fn to_json(&self) -> String {
let items: Vec<String> = self.iter().map(|item| item.to_json()).collect();
format!("[{}]", items.join(", "))
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{ \"name\": {:?}, \"age\": {}, \"spec\": {} }}",
self.name,
self.age,
self.spec.to_json()
)
}
}
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()
)
}
}
impl ToJson for TeacherRole {
fn to_json(&self) -> String {
match self {
TeacherRole::Professor(teacher) => teacher.to_json(),
TeacherRole::StudentTeacher(student) => student.to_json(),
}
}
}
impl ToJson for University {
fn to_json(&self) -> String {
format!(
"{{ \"name\": {:?}, \"students\": {}, \"teachers\": {} }}",
self.name,
self.students.to_json(),
self.teachers.to_json()
)
}
}
fn main() {
let s1 = Student::new("Donald".to_string(), 22, Spec::SI);
let s2 = Student::new("Dafi".to_string(), 22, Spec::IS);
let t1 = Teacher::new(
"Petrov".to_string(),
62,
vec![Spec::SI, Spec::IS],
Title::Doctor,
);
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 unicode-ident v1.0.22
   Compiling quote v1.0.41
   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 futures-io v0.3.31
   Compiling syn v2.0.109
   Compiling futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1q9yt08/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:9:6
  |
9 | enum Title {
  |      ^^^^^

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

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

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

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

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

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

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

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

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

warning: function `main` is never used
   --> src/lib.rs:151:4
    |
151 | fn main() {
    |    ^^^^

warning: `solution` (lib) generated 12 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:5:13
  |
4 | enum Spec {
  |      ---- variants in this enum
5 |     SI, IS, KN, I, 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:10:5
   |
9  | enum Title {
   |      ----- variants in this enum
10 |     Assistant, Doctor, Professor,
   |     ^^^^^^^^^  ^^^^^^
   |
   = note: `Title` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis

warning: function `main` is never used
   --> tests/../src/lib.rs:151:4
    |
151 | fn main() {
    |    ^^^^

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

Владимир качи първо решение на 01.11.2025 17:25 (преди около 1 месеца)