Решение на упр.04 задача 1 от Мариян Момчилов
Към профила на Мариян Момчилов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
pub enum Spec {
SI,
IS,
KN,
I,
M,
}
#[derive(Debug)]
pub enum Title {
Assistant,
Doctor,
Professor,
}
#[derive(Debug)]
pub struct Student {
name: String,
age: usize,
spec: Spec,
}
#[derive(Debug)]
pub struct Teacher {
name: String,
age: usize,
spec: Vec<Spec>,
title: Title,
}
#[derive(Debug)]
pub struct University<T: ToJson> {
name: String,
students: Vec<Student>,
teachers: Vec<T>,
}
impl Student {
pub fn new(name: String, age: usize, spec: Spec) -> Self {
Student { name, age, spec }
}
}
impl Teacher {
pub fn new(name: String, age: usize, spec: Vec<Spec>, title: Title) -> Self {
Teacher {
name,
age,
spec,
title,
}
}
}
impl<T: ToJson> University<T> {
pub fn new(name: String, students: Vec<Student>, teachers: Vec<T>) -> Self {
University {
name,
students,
teachers,
}
}
}
pub trait ToJson {
fn to_json(&self) -> String;
}
impl ToJson for Title {
fn to_json(&self) -> String {
String::from(match *self {
Title::Assistant => "\"Assistant\"",
Title::Doctor => "\"Doctor\"",
Title::Professor => "\"Professor\"",
})
}
}
impl ToJson for Spec {
fn to_json(&self) -> String {
String::from(match *self {
Spec::SI => "\"SI\"",
Spec::KN => "\"KN\"",
Spec::I => "\"I\"",
Spec::IS => "\"IS\"",
Spec::M => "M",
})
}
}
impl ToJson for Student {
fn to_json(&self) -> String {
format!(
"{}\"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 result = String::from("[");
let mut it = self.iter();
let mut el: Option<&T> = it.next();
loop {
match el {
Some(s) => {
result.push_str(&s.to_json());
}
None => {
break;
}
};
el = it.next();
match el {
Some(_) => {
result.push(',');
}
None => {
break;
}
};
}
result.push(']');
result
}
}
impl ToJson for str {
fn to_json(&self) -> String {
format!("\"{}\"", self)
}
}
impl ToJson for usize {
fn to_json(&self) -> String {
self.to_string()
}
}
impl ToJson for Teacher {
fn to_json(&self) -> String {
format!(
"{}\"name\":{},\"age\":{},\"spec\":{},\"title\":{}{}",
"{",
&self.name.to_json(),
&self.age.to_json(),
&self.spec.to_json(),
&self.title.to_json(),
"}"
)
}
}
impl<T: ToJson> ToJson for University<T> {
fn to_json(&self) -> String {
format!(
"{}\"name\":{},\"students\":{},\"teachers\":{}{}",
"{",
&self.name.to_json(),
&self.students.to_json(),
&self.teachers.to_json(),
"}"
)
}
}
fn main() {
let ivan = Student::new("Ivan".to_string(), 23, Spec::KN);
let gosho = Teacher::new(
"Gosho".to_string(),
25,
vec![Spec::KN, Spec::IS],
Title::Assistant,
);
let pesho = Student::new("Pesho".to_string(), 44, Spec::I);
let mitko = Student::new("Mitko".to_string(), 45, Spec::I);
let unibit = University::new("Unibit".to_string(), vec![ivan], vec![gosho]);
let fmi = University::new("FMI".to_string(), vec![pesho], vec![mitko]);
println!("{}", unibit.to_json());
println!("{}", fmi.to_json());
}
Лог от изпълнението
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-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling slab v0.4.11
Compiling syn v2.0.109
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling memchr v2.7.6
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling solution v0.1.0 (/tmp/d20251106-1757769-12kvg3/solution)
warning: function `main` is never used
--> src/lib.rs:173:4
|
173 | fn main() {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (lib) generated 1 warning
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 | pub 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 | pub 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:173:4
|
173 | fn main() {
| ^^^^
warning: `solution` (test "solution_test") generated 3 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.46s
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
