Решение на упр.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: usize,
spec: Spec,
}
#[derive(Debug)]
struct Teacher {
name: String,
age: usize,
spec: Vec<Spec>,
title: Title,
}
#[derive(Debug)]
enum AcademicPerson {
Student(Student),
Teacher(Teacher),
}
#[derive(Debug)]
struct University {
name: String,
students: Vec<Student>,
teachers: Vec<AcademicPerson>,
}
trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for String {
fn to_json(&self) -> String {
format!("\"{}\"", self)
}
}
impl Spec {
fn as_string(&self) -> String {
match self {
Spec::SI => String::from("SI"),
Spec::IS => String::from("IS"),
Spec::KN => String::from("KN"),
Spec::I => String::from("I"),
Spec::M => String::from("M"),
}
}
}
impl ToJson for Spec {
fn to_json(&self) -> String {
self.as_string().to_json()
}
}
impl Title {
fn as_string(&self) -> String {
match self {
Title::Assistant => String::from("Assistant"),
Title::Doctor => String::from("Doctor"),
Title::Professor => String::from("Professor"),
}
}
}
impl ToJson for Title {
fn to_json(&self) -> String {
self.as_string().to_json()
}
}
impl Student {
pub fn new(name: String, age: usize, spec: Spec) -> Self {
Student { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\": {}, \"age\": {}, \"spec\": {}}}",
self.name.to_json(),
self.age,
self.spec.to_json()
)
}
}
impl Teacher {
pub fn new(name: String, age: usize, spec: Vec<Spec>, title: Title) -> Self {
Teacher {
name,
age,
spec,
title,
}
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
format!(
"{{\"name\": {}, \"age\": {}, \"spec\": {}, \"title\": {}}}",
self.name.to_json(),
self.age,
self.spec.to_json(),
self.title.to_json()
)
}
}
impl ToJson for AcademicPerson {
fn to_json(&self) -> String {
match self {
AcademicPerson::Student(student) => student.to_json(),
AcademicPerson::Teacher(teacher) => teacher.to_json(),
}
}
}
impl From<Student> for AcademicPerson {
fn from(student: Student) -> Self {
Self::Student(student)
}
}
impl From<Teacher> for AcademicPerson {
fn from(teacher: Teacher) -> Self {
Self::Teacher(teacher)
}
}
impl University {
pub fn new<T: Into<AcademicPerson>>(
name: String,
students: Vec<Student>,
teachers: Vec<T>,
) -> Self {
University {
name,
students,
teachers: teachers.into_iter().map(|x| x.into()).collect(),
}
}
}
impl ToJson for University {
fn to_json(&self) -> String {
format!(
"{{\"name\": {}, \"students\": {}, \"teachers\": {}}}",
self.name.to_json(),
self.students.to_json(),
self.teachers.to_json()
)
}
}
impl<T: ToJson> ToJson for Vec<T> {
fn to_json(&self) -> String {
let mut result = String::from("[");
let inner_str = self
.iter()
.map(|x| x.to_json())
.collect::<Vec<String>>()
.join(", ");
result.push_str(&inner_str);
result.push(']');
result
}
}
fn main() {
let student = Student::new("Dimitar".to_string(), 20, Spec::KN);
let teacher = Teacher::new(
"Anonymous".to_string(),
27,
vec![Spec::KN],
Title::Assistant,
);
let university = University::new("SU".to_string(), vec![student], vec![teacher]);
println!("{}", university.to_json());
// {
// "name": "SU",
// "students": [
// {"name": "Dimitar", "age": 20, "spec": "KN"}
// ],
// "teachers": [
// {"name": "Anonymous", "age": 27, "spec": ["KN"], "title": "Assistant"}
// ]
// }
}

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

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 pin-project-lite v0.2.16
   Compiling futures-io v0.3.31
   Compiling memchr v2.7.6
   Compiling futures-task v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1ck61cy/solution)
warning: variants `SI`, `IS`, `KN`, `I`, and `M` are never constructed
 --> src/lib.rs:3:5
  |
2 | enum Spec {
  |      ---- variants in this enum
3 |     SI,
  |     ^^
4 |     IS,
  |     ^^
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`, `Doctor`, and `Professor` are never constructed
  --> src/lib.rs:12:5
   |
11 | enum Title {
   |      ----- variants in this enum
12 |     Assistant,
   |     ^^^^^^^^^
13 |     Doctor,
   |     ^^^^^^
14 |     Professor,
   |     ^^^^^^^^^
   |
   = note: `Title` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: fields `name`, `age`, and `spec` are never read
  --> src/lib.rs:19:5
   |
18 | struct Student {
   |        ------- fields in this struct
19 |     name: String,
   |     ^^^^
20 |     age: usize,
   |     ^^^
21 |     spec: Spec,
   |     ^^^^
   |
   = note: `Student` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

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

warning: field `0` is never read
  --> src/lib.rs:34:13
   |
34 |     Student(Student),
   |     ------- ^^^^^^^
   |     |
   |     field in this variant
   |
   = note: `AcademicPerson` 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
   |
34 |     Student(()),
   |             ~~

warning: field `0` is never read
  --> src/lib.rs:35:13
   |
35 |     Teacher(Teacher),
   |     ------- ^^^^^^^
   |     |
   |     field in this variant
   |
   = note: `AcademicPerson` 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
   |
35 |     Teacher(()),
   |             ~~

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

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

warning: method `as_string` is never used
  --> src/lib.rs:56:8
   |
55 | impl Spec {
   | --------- method in this implementation
56 |     fn as_string(&self) -> String {
   |        ^^^^^^^^^

warning: method `as_string` is never used
  --> src/lib.rs:74:8
   |
73 | impl Title {
   | ---------- method in this implementation
74 |     fn as_string(&self) -> String {
   |        ^^^^^^^^^

warning: associated function `new` is never used
  --> src/lib.rs:90:12
   |
89 | impl Student {
   | ------------ associated function in this implementation
90 |     pub fn new(name: String, age: usize, spec: Spec) -> Self {
   |            ^^^

warning: associated function `new` is never used
   --> src/lib.rs:107:12
    |
106 | impl Teacher {
    | ------------ associated function in this implementation
107 |     pub fn new(name: String, age: usize, spec: Vec<Spec>, title: Title) -> Self {
    |            ^^^

warning: associated function `new` is never used
   --> src/lib.rs:151:12
    |
150 | impl University {
    | --------------- associated function in this implementation
151 |     pub fn new<T: Into<AcademicPerson>>(
    |            ^^^

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

warning: `solution` (lib) generated 14 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:189:4
    |
189 | fn main() {
    |    ^^^^

warning: `solution` (test "solution_test") generated 3 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 10.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 23:55 (преди около 1 месеца)