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

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

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

Резултати

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

Код

trait ToJson {
fn to_json(&self) -> String;
}
#[derive(Debug)]
enum Spec{
SI,
IS,
KN,
I,
M,
}
impl ToJson for Spec {
fn to_json(&self) -> String {
let text = match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
};
format!("\"{}\"", text)
}
}
#[derive(Debug)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl ToJson for Title {
fn to_json(&self) -> String {
let text = match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
};
format!("\"{}\"", text)
}
}
#[derive(Debug)]
struct Student {
name: String,
age: u8,
spec: Spec
}
impl Student {
fn new(name: String, age: u8, spec: Spec) -> Self {
Self { 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: u8,
specs: Vec<Spec>,
title: Title
}
impl Teacher {
fn new(name: String, age: u8, specs: Vec<Spec>, title: Title) -> Self {
Self { name, age, specs, title }
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let mut specs_json = "[".to_string();
for (i, spec) in self.specs.iter().enumerate() {
specs_json.push_str(&spec.to_json());
if i < self.specs.len() - 1 {
specs_json.push(',');
}
}
specs_json.push(']');
format!("{{\"name\":\"{}\",\"age\":{},\"spec\":{},\"title\":{}}}",
self.name, self.age, specs_json, self.title.to_json())
}
}
#[derive(Debug)]
struct University<S, T> {
name: String,
students: Vec<S>,
teachers: Vec<T>,
}
impl<S, T> University<S, T> {
fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> Self {
Self { name, students, teachers }
}
}
impl<S: ToJson, T: ToJson> ToJson for University<S, T> {
fn to_json(&self) -> String {
let mut students_json = "[".to_string();
for (i, s) in self.students.iter().enumerate() {
students_json.push_str(&s.to_json());
if i < self.students.len() - 1 {
students_json.push(',');
}
}
students_json.push(']');
let mut teachers_json = "[".to_string();
for (i, t) in self.teachers.iter().enumerate() {
teachers_json.push_str(&t.to_json());
if i < self.teachers.len() - 1 {
teachers_json.push(',');
}
}
teachers_json.push(']');
format!("{{\"name\":\"{}\",\"students\":{},\"teachers\":{}}}",
self.name, students_json, teachers_json)
}
}
fn main() {
let s1 = Student::new("Ivan".to_string(), 20, Spec::SI);
let s2 = Student::new("Peter".to_string(), 25, Spec::SI);
let t1 = Teacher::new(
"Dr. Petrov".to_string(),
45,
vec![Spec::SI, Spec::IS],
Title::Professor,
);
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 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 memchr v2.7.6
   Compiling syn v2.0.109
   Compiling slab v0.4.11
   Compiling pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1u4ex75/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:26:6
   |
26 | enum Title {
   |      ^^^^^

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

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

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

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

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

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

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

warning: `solution` (lib) generated 10 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:27:5
   |
26 | enum Title {
   |      ----- variants in this enum
27 |     Assistant,
   |     ^^^^^^^^^
28 |     Doctor,
   |     ^^^^^^
   |
   = note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

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

warning: `solution` (test "solution_test") generated 3 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.39s
     Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)

running 2 tests
test solution_test::test_basic ... ok
test solution_test::test_basic2 ... 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:57 (преди около 1 месеца)