Решение на упр.04 задача 1 от Георги Стоянов

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

Към профила на Георги Стоянов

Резултати

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

Код

#[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: i32,
spec: Spec,
}
impl Student {
fn new(name: String, age: i32, spec: Spec) -> Self {
Student {
name,
age,
spec,
}
}
}
#[derive(Debug, Clone)]
struct Teacher {
name: String,
age: i32,
specs: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: i32, specs: Vec<Spec>, title: Title) -> Self {
Teacher {
name,
age,
specs,
title,
}
}
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Student>,
teachers: Vec<Teacher>,
}
impl University {
fn new(name: String, students:Vec<Student>, teachers:Vec<Teacher>) -> Self {
University {
name,
students: students,
teachers: teachers,
}
}
}
trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for University {
fn to_json(&self) -> String {
let mut res = String::from("{\n");
res.push_str(&format!(" \"name\": \"{}\",\n", self.name));
res.push_str(" \"students\": [\n");
for (i, s) in self.students.iter().enumerate() {
res.push_str(&format!(
" {{\"name\": \"{}\", \"age\": {}, \"spec\": \"{:?}\"}}{}",
s.name,
s.age,
s.spec,
if i + 1 == self.students.len() {
"\n"
} else {
",\n"
}
));
}
res.push_str(" ],\n");
res.push_str(" \"teachers\": [\n");
for (i, t) in self.teachers.iter().enumerate() {
let specs_str: Vec<String> = t.specs.iter().map(|s| format!("\"{:?}\"", s)).collect();
let specs_json = specs_str.join(", ");
res.push_str(&format!(
" {{\"name\": \"{}\", \"age\": {}, \"title\": \"{:?}\", \"specs\": [{}]}}{}",
t.name,
t.age,
t.title,
specs_json,
if i + 1 == self.teachers.len() {
"\n"
} else {
",\n"
}
));
}
res.push_str(" ]\n");
res.push('}');
res
}
}

Лог от изпълнението

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-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling syn v2.0.109
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-w68zb5/solution)
warning: enum `Spec` is never used
 --> src/lib.rs:2:6
  |
2 | enum Spec {
  |      ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

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

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

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

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

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

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

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

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

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
error[E0308]: mismatched types
  --> tests/solution_test.rs:40:64
   |
40 |     let uni = University::new("SU".to_string(), vec![s1], vec![s2]);
   |                                                                ^^ expected `Teacher`, found `Student`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `solution` (test "solution_test") due to 1 previous error

История (1 версия и 0 коментара)

Георги качи първо решение на 31.10.2025 15:30 (преди около 1 месеца)