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

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

Към профила на Борис Великов

Резултати

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

Код

trait ToJson{
fn to_json(&self) -> String;
}
#[derive(Debug)]
enum Spec{SI,IS,KN,I,M}
#[derive(Debug)]
enum Title{Assistant, Doctor, Professor}
#[derive(Debug)]
struct Student{
name : String,
age : u32,
programme : Spec
}
impl Student{
fn new(name: String, age: u32, programme: Spec) -> Self {
Self { name, age, programme }
}
}
#[derive(Debug)]
struct Teacher{
name : String,
age : u32,
teach_list : Vec<Spec>,
zvanie : Title
}
impl Teacher{
fn new(name: String, age: u32, teach_list: Vec<Spec>, zvanie: Title) -> Self {
Self { name, age, teach_list, zvanie }
}
}
#[derive(Debug)]
enum Staff {
Teacher(Teacher),
Student(Student),
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Student>,
teachers: Vec<Staff>
}
impl ToJson for Spec {
fn to_json(&self) -> String {
match self {
Spec::SI => "\"SI\"".to_string(),
Spec::IS => "\"IS\"".to_string(),
Spec::KN => "\"KN\"".to_string(),
Spec::I => "\"I\"".to_string(),
Spec::M => "\"M\"".to_string(),
}
}
}
impl ToJson for Title {
fn to_json(&self) -> String {
match self {
Title::Assistant => "\"Assistant\"".to_string(),
Title::Doctor => "\"Doctor\"".to_string(),
Title::Professor => "\"Professor\"".to_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)
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\": \"{}\", \"age\": {}, \"programme\": {}}}",
self.name,
self.age,
self.programme.to_json()
)
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
format!(
"{{\"name\": \"{}\", \"age\": {}, \"spec\": {}, \"title\": \"{:?}\"}}",
self.name,
self.age,
self.teach_list.to_json(),
self.zvanie
)
}
}
impl ToJson for Staff {
fn to_json(&self) -> String {
match self {
Staff::Teacher(teacher) => teacher.to_json(),
Staff::Student(student) => student.to_json(),
}
}
}
impl ToJson for University {
fn to_json(&self) -> String {
format!(
"{{\"name\": \"{}\",\"students\": {}, \"teachers\": {}}}",
self.name,
self.students.to_json(),
self.teachers.to_json()
)
}
}
// Add these conversion implementations
impl From<Teacher> for Staff {
fn from(teacher: Teacher) -> Self {
Staff::Teacher(teacher)
}
}
impl From<Student> for Staff {
fn from(student: Student) -> Self {
Staff::Student(student)
}
}
// Then use any of the University::new implementations above
impl University {
fn new<T: Into<Staff>>(name: String, students: Vec<Student>, staff: Vec<T>) -> Self {
Self {
name,
students,
teachers: staff.into_iter().map(Into::into).collect(),
}
}
}
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 pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling memchr v2.7.6
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-l6vaig/solution)
warning: trait `ToJson` is never used
 --> src/lib.rs:1:7
  |
1 | trait ToJson{
  |       ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: variants `SI`, `IS`, `KN`, `I`, and `M` are never constructed
 --> src/lib.rs:6:11
  |
6 | enum Spec{SI,IS,KN,I,M}
  |      ---- ^^ ^^ ^^ ^ ^
  |      |
  |      variants in this enum
  |
  = note: `Spec` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: variants `Assistant`, `Doctor`, and `Professor` are never constructed
 --> src/lib.rs:9:12
  |
9 | enum Title{Assistant, Doctor, Professor}
  |      ----- ^^^^^^^^^  ^^^^^^  ^^^^^^^^^
  |      |
  |      variants in this enum
  |
  = note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: fields `name`, `age`, and `programme` are never read
  --> src/lib.rs:13:5
   |
12 | struct Student{
   |        ------- fields in this struct
13 |     name        : String,
   |     ^^^^
14 |     age         : u32,
   |     ^^^
15 |     programme   : Spec
   |     ^^^^^^^^^
   |
   = note: `Student` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

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

warning: fields `name`, `age`, `teach_list`, and `zvanie` are never read
  --> src/lib.rs:26:5
   |
25 | struct Teacher{
   |        ------- fields in this struct
26 |     name        : String,
   |     ^^^^
27 |     age         : u32,
   |     ^^^
28 |     teach_list  : Vec<Spec>,
   |     ^^^^^^^^^^
29 |     zvanie      : Title
   |     ^^^^^^
   |
   = note: `Teacher` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: associated function `new` is never used
  --> src/lib.rs:33:8
   |
32 | impl Teacher{
   | ------------ associated function in this implementation
33 |     fn new(name: String, age: u32, teach_list: Vec<Spec>, zvanie: Title) -> Self {
   |        ^^^

warning: field `0` is never read
  --> src/lib.rs:40:13
   |
40 |     Teacher(Teacher),
   |     ------- ^^^^^^^
   |     |
   |     field in this variant
   |
   = note: `Staff` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
   |
40 |     Teacher(()),
   |             ~~

warning: field `0` is never read
  --> src/lib.rs:41:13
   |
41 |     Student(Student),
   |     ------- ^^^^^^^
   |     |
   |     field in this variant
   |
   = note: `Staff` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
   |
41 |     Student(()),
   |             ~~

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

warning: associated function `new` is never used
   --> src/lib.rs:151:8
    |
150 | impl University {
    | --------------- associated function in this implementation
151 |     fn new<T: Into<Staff>>(name: String, students: Vec<Student>, staff: Vec<T>) -> Self {
    |        ^^^

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

warning: `solution` (lib) generated 12 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:6:17
  |
6 | enum Spec{SI,IS,KN,I,M}
  |      ----       ^^ ^ ^
  |      |
  |      variants in this enum
  |
  = 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:9:12
  |
9 | enum Title{Assistant, Doctor, Professor}
  |      ----- ^^^^^^^^^  ^^^^^^
  |      |
  |      variants in this enum
  |
  = 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:160:4
    |
160 | fn main(){
    |    ^^^^

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

running 2 tests
test solution_test::test_basic ... FAILED
test solution_test::test_basic2 ... FAILED

failures:

---- solution_test::test_basic stdout ----
thread 'solution_test::test_basic' panicked at tests/solution_test.rs:32:5:
assertion `left == right` failed
  left: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"programme\":\"SI\"},{\"name\":\"Peter\",\"age\":25,\"programme\":\"SI\"}],\"teachers\":[{\"name\":\"Dr.Petrov\",\"age\":45,\"spec\":[\"SI\",\"IS\"],\"title\":\"Professor\"}]}"
 right: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"spec\":\"SI\"},{\"name\":\"Peter\",\"age\":25,\"spec\":\"SI\"}],\"teachers\":[{\"name\":\"Dr.Petrov\",\"age\":45,\"spec\":[\"SI\",\"IS\"],\"title\":\"Professor\"}]}"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_basic2 stdout ----
thread 'solution_test::test_basic2' panicked at tests/solution_test.rs:52:5:
assertion `left == right` failed
  left: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"programme\":\"SI\"}],\"teachers\":[{\"name\":\"Peter\",\"age\":25,\"programme\":\"SI\"}]}"
 right: "{\"name\":\"SU\",\"students\":[{\"name\":\"Ivan\",\"age\":20,\"spec\":\"SI\"}],\"teachers\":[{\"name\":\"Peter\",\"age\":25,\"spec\":\"SI\"}]}"


failures:
    solution_test::test_basic
    solution_test::test_basic2

test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

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

Борис качи първо решение на 04.11.2025 12:16 (преди около 1 месеца)