Решение на упр.04 задача 1 от Стилиян Иванов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::fmt::Debug;
trait ToJson {
fn to_json(&self) -> String;
}
#[allow(dead_code)]
#[derive(Debug)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToJson for Spec {
fn to_json(&self) -> String {
let s = match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
};
format!("\"{}\"", s)
}
}
#[allow(dead_code)]
#[derive(Debug)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl ToJson for Title {
fn to_json(&self) -> String {
let s = match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
};
format!("\"{}\"", s)
}
}
#[derive(Debug)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":{}}}",
self.name,
self.age,
self.spec.to_json()
)
}
}
#[derive(Debug)]
struct Teacher {
name: String,
age: u32,
specs: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Self { name, age, specs, title }
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json: Vec<String> = self.specs.iter()
.map(|s| s.to_json())
.collect();
let specs_str = format!("[{}]", specs_json.join(","));
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":{},\"title\":{}}}",
self.name,
self.age,
specs_str,
self.title.to_json()
)
}
}
#[derive(Debug)]
struct University<S, T> {
name: String,
students: Vec<S>,
teachers: Vec<T>,
}
impl<S: ToJson + Debug, T: ToJson + Debug> University<S, T> {
fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> Self {
Self { name, students, teachers }
}
}
impl<S: ToJson, T: ToJson> ToJson for University<S, T> {
fn to_json(&self) -> String {
let students_json: Vec<String> = self.students.iter()
.map(|s| s.to_json())
.collect();
let students_str = format!("[{}]", students_json.join(","));
let teachers_json: Vec<String> = self.teachers.iter()
.map(|t| t.to_json())
.collect();
let teachers_str = format!("[{}]", teachers_json.join(","));
format!(
"{{\"name\":\"{}\",\"students\":{},\"teachers\":{}}}",
self.name,
students_str,
teachers_str
)
}
}
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 futures-io v0.3.31
Compiling pin-project-lite v0.2.16
Compiling syn v2.0.109
Compiling memchr v2.7.6
Compiling futures-task v0.3.31
Compiling pin-utils v0.1.0
Compiling solution v0.1.0 (/tmp/d20251106-1757769-a78w4a/solution)
warning: trait `ToJson` is never used
--> src/lib.rs:3:7
|
3 | trait ToJson {
| ^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: struct `Student` is never constructed
--> src/lib.rs:50:8
|
50 | struct Student {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:57:8
|
56 | impl Student {
| ------------ associated function in this implementation
57 | fn new(name: String, age: u32, spec: Spec) -> Self {
| ^^^
warning: struct `Teacher` is never constructed
--> src/lib.rs:74:8
|
74 | struct Teacher {
| ^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:82:8
|
81 | impl Teacher {
| ------------ associated function in this implementation
82 | fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
| ^^^
warning: struct `University` is never constructed
--> src/lib.rs:105:8
|
105 | struct University<S, T> {
| ^^^^^^^^^^
warning: associated function `new` is never used
--> src/lib.rs:112:8
|
111 | impl<S: ToJson + Debug, T: ToJson + Debug> University<S, T> {
| ----------------------------------------------------------- associated function in this implementation
112 | fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> Self {
| ^^^
warning: function `main` is never used
--> src/lib.rs:138:4
|
138 | fn main() {
| ^^^^
warning: `solution` (lib) generated 8 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: function `main` is never used
--> tests/../src/lib.rs:138:4
|
138 | fn main() {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (test "solution_test") generated 1 warning
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.42s
Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)
running 2 tests
test solution_test::test_basic ... ok
test solution_test::test_basic2 ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
История (2 версии и 0 коментара)
Стилиян качи решение на 01.11.2025 16:39 (преди около 1 месеца)
use std::fmt::Debug;
trait ToJson {
fn to_json(&self) -> String;
}
+#[allow(dead_code)]
#[derive(Debug)]
-#[warn(dead_code)]
enum Spec {
SI,
IS,
KN,
I,
M,
}
impl ToJson for Spec {
fn to_json(&self) -> String {
let s = match self {
Spec::SI => "SI",
Spec::IS => "IS",
Spec::KN => "KN",
Spec::I => "I",
Spec::M => "M",
};
format!("\"{}\"", s)
}
}
+#[allow(dead_code)]
#[derive(Debug)]
-#[warn(dead_code)]
enum Title {
Assistant,
Doctor,
Professor,
}
impl ToJson for Title {
fn to_json(&self) -> String {
let s = match self {
Title::Assistant => "Assistant",
Title::Doctor => "Doctor",
Title::Professor => "Professor",
};
format!("\"{}\"", s)
}
}
#[derive(Debug)]
struct Student {
name: String,
age: u32,
spec: Spec,
}
impl Student {
fn new(name: String, age: u32, spec: Spec) -> Self {
Self { name, age, spec }
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":{}}}",
self.name,
self.age,
self.spec.to_json()
)
}
}
#[derive(Debug)]
struct Teacher {
name: String,
age: u32,
specs: Vec<Spec>,
title: Title,
}
impl Teacher {
fn new(name: String, age: u32, specs: Vec<Spec>, title: Title) -> Self {
Self { name, age, specs, title }
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
let specs_json: Vec<String> = self.specs.iter()
.map(|s| s.to_json())
.collect();
let specs_str = format!("[{}]", specs_json.join(","));
format!(
"{{\"name\":\"{}\",\"age\":{},\"spec\":{},\"title\":{}}}",
self.name,
self.age,
specs_str,
self.title.to_json()
)
}
}
#[derive(Debug)]
struct University<S, T> {
name: String,
students: Vec<S>,
teachers: Vec<T>,
}
impl<S: ToJson + Debug, T: ToJson + Debug> University<S, T> {
fn new(name: String, students: Vec<S>, teachers: Vec<T>) -> Self {
Self { name, students, teachers }
}
}
impl<S: ToJson, T: ToJson> ToJson for University<S, T> {
fn to_json(&self) -> String {
let students_json: Vec<String> = self.students.iter()
.map(|s| s.to_json())
.collect();
let students_str = format!("[{}]", students_json.join(","));
let teachers_json: Vec<String> = self.teachers.iter()
.map(|t| t.to_json())
.collect();
let teachers_str = format!("[{}]", teachers_json.join(","));
format!(
"{{\"name\":\"{}\",\"students\":{},\"teachers\":{}}}",
self.name,
students_str,
teachers_str
)
}
}
+
+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());
+}
+
