Решение на упр.04 задача 1 от Йосиф Хамед

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

Към профила на Йосиф Хамед

Резултати

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

Код

trait ToJson {
fn to_json(&self) -> String;
}
impl<T> ToJson for Vec<T>
where
T: ToJson,
{
fn to_json(&self) -> String {
let mut iter = self.iter();
let mut result = match iter.next() {
Some(first) => first.to_json(),
None => String::new(),
};
for e in iter {
result.push_str(", ");
result.push_str(&e.to_json());
}
format!("[{}]", result)
}
}
pub enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToJson for Spec {
fn to_json(&self) -> String {
match self {
Spec::SI => "\"SI\"",
Spec::IS => "\"IS\"",
Spec::KN => "\"KN\"",
Spec::I => "\"I\"",
Spec::M => "\"M\"",
}
.to_string()
}
}
pub enum Title {
Professor,
Assistant,
Doctor,
}
impl ToJson for Title {
fn to_json(&self) -> String {
match self {
Title::Professor => "\"Professor\"",
Title::Assistant => "\"Assistant\"",
Title::Doctor => "\"Doctor\"",
}
.to_string()
}
}
pub struct Student {
name: String,
age: i32,
s: Spec,
}
impl Student {
fn new(name: String, age: i32, s: Spec) -> Self {
Self { name, age, s }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
r#"{{
"name": "{}",
"age": {},
"spec": {}
}}"#,
self.name,
self.age,
self.s.to_json()
)
}
}
pub struct Teacher {
name: String,
age: i32,
s: Vec<Spec>,
t: Title,
}
impl Teacher {
fn new(name: String, age: i32, s: Vec<Spec>, t: Title) -> Self {
Self { name, age, s, t }
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
format!(
r#"{{
"name": "{}",
"age": {},
"spec": {},
"title": {}
}}"#,
self.name,
self.age,
self.s.to_json(),
self.t.to_json()
)
}
}
impl ToJson for Box<dyn ToJson> {
fn to_json(&self) -> String {
(**self).to_json()
}
}
struct University<T: ToJson> {
name: String,
s: Vec<Student>,
t: Vec<T>,
}
impl<T: ToJson> University<T> {
fn new(name: String, s: Vec<Student>, t: Vec<T>) -> Self {
Self { name, s, t }
}
}
impl<T: ToJson> ToJson for University<T> {
fn to_json(&self) -> String {
format!(
" {{\"name\": \"{}\", \"students\": {}, \"teachers\": {} }}",
self.name,
self.s.to_json(),
self.t.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 futures-task v0.3.31
   Compiling pin-utils v0.1.0
   Compiling syn v2.0.109
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-zlvutz/solution)
warning: trait `ToJson` is never used
 --> src/lib.rs:1:7
  |
1 | trait ToJson {
  |       ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: fields `name`, `age`, and `s` are never read
  --> src/lib.rs:62:5
   |
61 | pub struct Student {
   |            ------- fields in this struct
62 |     name: String,
   |     ^^^^
63 |     age: i32,
   |     ^^^
64 |     s: Spec,
   |     ^

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

warning: fields `name`, `age`, `s`, and `t` are never read
  --> src/lib.rs:88:5
   |
87 | pub struct Teacher {
   |            ------- fields in this struct
88 |     name: String,
   |     ^^^^
89 |     age: i32,
   |     ^^^
90 |     s: Vec<Spec>,
   |     ^
91 |     t: Title,
   |     ^

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

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

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

warning: `solution` (lib) generated 7 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:29:5
   |
26 | pub enum Spec {
   |          ---- variants in this enum
...
29 |     KN,
   |     ^^
30 |     I,
   |     ^
31 |     M,
   |     ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: variants `Assistant` and `Doctor` are never constructed
  --> tests/../src/lib.rs:47:5
   |
45 | pub enum Title {
   |          ----- variants in this enum
46 |     Professor,
47 |     Assistant,
   |     ^^^^^^^^^
48 |     Doctor,
   |     ^^^^^^

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

Йосиф качи първо решение на 04.11.2025 01:37 (преди около 1 месеца)