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

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

Към профила на Илиян Свечев

Резултати

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

Код

#[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,
}
impl Student {
fn new(name: String, age: u8, spec: Spec) -> Student {
Student{name, age, spec}
}
}
#[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) -> Teacher {
Teacher{name, age, specs, title}
}
}
#[derive(Debug)]
struct University<T, U> {
name: String,
students: Vec<T>,
teachers: Vec<U>,
}
impl<T, U> University<T, U> {
fn new(name: String, students: Vec<T>, teachers: Vec<U>) -> University<T, U> {
University { name, students, teachers }
}
}
trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for Spec {
fn to_json(&self) -> String {
match &&self {
Self::I => {
format!("\"I\"")
}
Self::IS => {
format!("\"IS\"")
}
Self::KN => {
format!("\"KN\"")
}
Self::M => {
format!("\"M\"")
}
Self::SI => {
format!("\"SI\"")
}
}
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
//format!("{{\"name\": \"{}\", \"age\": {}, \"spec\": \"{:?}\"\\}}", self.name, self.age, self.spec)
format!(r#"{{"name": "{}", "age": {}, "spec": "{:?}"}}"#, self.name, self.age, self.spec)
}
}
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)
}
}
impl<T: ToJson> ToJson for Vec<T> {
fn to_json(&self) -> String {
let mut res = "[".to_string();
for person in self {
res.push_str(person.to_json().as_str());
res.push_str(",\n");
}
res.pop(); // remove ,\n after the last person
res.pop();
res.push_str("]");
res
}
}
impl<T: ToJson, U: ToJson> ToJson for University<T, U> {
fn to_json(&self) -> String {
format!(
r#"{{
"name": "{}",
"students": {},
"teachers": {}
}}"#,
self.name, self.students.to_json(), self.teachers.to_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 unicode-ident v1.0.22
   Compiling quote v1.0.41
   Compiling futures-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling slab v0.4.11
   Compiling syn v2.0.109
   Compiling memchr v2.7.6
   Compiling futures-io v0.3.31
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1b6xevp/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: u8, spec: Spec) -> Student {
   |        ^^^

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

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

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

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

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

warning: function `main` is never used
   --> src/lib.rs:124:4
    |
124 | 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:5:5
  |
2 | enum Spec {
  |      ---- variants in this enum
...
5 |     KN,
  |     ^^
6 |     I,
  |     ^
7 |     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:12:5
   |
11 | enum Title {
   |      ----- variants in this enum
12 |     Assistant,
   |     ^^^^^^^^^
13 |     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:124:4
    |
124 | fn main() {
    |    ^^^^

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

Илиян качи първо решение на 30.10.2025 22:13 (преди около 1 месеца)