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

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

Към профила на Йоан Грозев

Резултати

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

Код

trait ToJson
{
fn to_json(&self)->String;
}
enum Spec
{
SI,
IS,
KN,
I,
M,
}
enum Title
{
Assistant,
Doctor,
Professor,
}
struct Student
{
name: String,
age: u32,
spec: Spec,
}
struct Teacher
{
name: String,
age: u32,
specs: Vec<Spec>,
title:Title
}
struct University
{
name: String,
students: Vec<Student>,
teachers: Vec<Teacher>,
}
impl ToJson for u32
{
fn to_json(&self)->String
{
format!("{}",self)
}
}
impl ToJson for String
{
fn to_json(&self)->String
{
format!("\"{}\"",self)
}
}
impl ToJson for Spec
{
fn to_json(&self)->String
{
match self
{
Spec::SI =>return format!("{}","\"SI\"".to_string()),
Spec::IS =>return format!("{}","\"IS\"".to_string()),
Spec::KN =>return format!("{}","\"KN\"".to_string()),
Spec::I =>return format!("{}","\"I\"".to_string()),
Spec::M =>return format!("{}","\"M\"".to_string()),
};
}
}
impl ToJson for Title
{
fn to_json(&self)->String
{
match self
{
Title::Assistant =>return format!("{}","\"Assistant\"".to_string()),
Title::Doctor =>return format!("{}","\"Doctor\"".to_string()),
Title::Professor =>return format!("{}","\"Professor\"".to_string()),
};
}
}
impl ToJson for Student
{
fn to_json(&self)->String
{
format!(
r#"{{
"name": {},
"age": {},
"spec": {}
}}"#,
self.name.to_json(), self.age.to_json(),
self.spec.to_json()
)
}
}
impl<T: ToJson> ToJson for Vec<T>
{
fn to_json(&self)->String
{
let mut iterator = self.iter();
let mut result = match iterator.next()
{
Some(value) => value.to_json(),
None => String::new(),
};
for i in iterator
{
result.push_str(", ");
result.push_str(&i.to_json());
}
return format!("[{}]", result)
}
}
impl ToJson for Teacher
{
fn to_json(&self)->String
{
format!(
r#"{{
"name": {},
"age": {},
"spec": {},
"title": {}
}}"#,
self.name.to_json(), self.age.to_json(),
self.specs.to_json(), self.title.to_json())
}
}
impl ToJson for University
{
fn to_json(&self)->String
{
format!(
r#"{{
"name": {},
"students": {},
"teachers": {}
}}"#,
self.name.to_json(), self.students.to_json(),self.teachers.to_json())
}
}
impl Student
{
fn new(name:String,age:u32,spec:Spec)->Student
{
let stud = Student{name:name,age:age,spec:spec,};
stud
}
}
impl Teacher
{
fn new(name:String,age:u32,specs:Vec<Spec>,title:Title)->Teacher
{
let teach = Teacher{name:name,age:age,specs:specs,title:title,};
teach
}
}
impl University
{
fn new(name:String,studs:Vec<Student>,teachs:Vec<Teacher>)->University
{
let uni = University{name:name,teachers:teachs,students:studs};
uni
}
}

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

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-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling futures-task v0.3.31
   Compiling syn v2.0.109
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1l467py/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:15:6
   |
15 | enum Title
   |      ^^^^^

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

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

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

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

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

warning: associated function `new` is never used
   --> src/lib.rs:180:8
    |
178 | impl University
    | --------------- associated function in this implementation
179 | {
180 |     fn new(name:String,studs:Vec<Student>,teachs:Vec<Teacher>)->University
    |        ^^^

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 коментара)

Йоан качи първо решение на 05.11.2025 19:35 (преди около 1 месеца)