Решение на упр.04 задача 2 от Йоан Грозев
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 4 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Clone)]
struct Book {
title: String,
author: String,
year: u32,
rating: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CompareResult {
Less,
Equal,
Greater,
}
trait Compare {
fn compare(&self, other: &Self) -> CompareResult;
}
trait Filter {
fn matches(&self, query: &str) -> bool;
}
trait Aggregate {
type Output;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SortOrder {
Desc,
Asc,
}
impl Compare for Book
{
fn compare(&self, other: &Self) -> CompareResult
{
if self.year > other.year
{
return CompareResult::Greater
}
else if self.year == other.year
{
return CompareResult::Equal
}
else
{
CompareResult::Less
}
}
}
fn checkIFThere(test:String,word:&str)->bool
{
let vecText:Vec<char> = test.chars().collect();
let vecWord:Vec<char> = word.chars().collect();
let mut i = 0;
while i < vecText.len()
{
if i + vecWord.len() <= vecText.len() && vecText[i .. i+vecWord.len()] == vecWord[..]
{
return true
}
i = i+1;
}
return false
}
impl Filter for Book
{
fn matches(&self, query: &str) -> bool
{
if checkIFThere(self.title.to_string(),query) == true || checkIFThere(self.author.to_string(),query)==true
{
return true
}
false
}
}
impl Aggregate for Book
{
type Output = f32;
fn aggregate(items: &[Self]) -> Self::Output
{
let mut summ:f32 = 0.0;
let mut counter = 0;
for i in items
{
summ = summ+i.rating;
counter = counter+1;
}
summ/counter as f32
}
}
fn process_items<T>(items: Vec<T>, query: &str, order: SortOrder) -> (Vec<T>, T::Output)
where
T: Compare + Filter + Aggregate + Clone,
{
let mut books: Vec<T> = items
.into_iter()
.filter(|x| x.matches(query))
.collect();
let countOfBooks = books.len();
for i in 0..countOfBooks
{
for j in i+1 .. countOfBooks
{
if books[i].compare(&books[j]) == CompareResult::Greater && order == SortOrder::Asc
{
books.swap(i,j);
}
else if books[i].compare(&books[j]) == CompareResult::Less && order == SortOrder::Desc
{
books.swap(i,j);
}
}
}
let aggregateValue = T::aggregate(&books);
(books, aggregateValue)
}
Лог от изпълнението
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 futures-io v0.3.31
Compiling pin-project-lite v0.2.16
Compiling syn v2.0.109
Compiling pin-utils v0.1.0
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling futures-task v0.3.31
Compiling solution v0.1.0 (/tmp/d20251106-1757769-1uwef4g/solution)
warning: struct `Book` is never constructed
--> src/lib.rs:2:8
|
2 | struct Book {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: enum `CompareResult` is never used
--> src/lib.rs:10:6
|
10 | enum CompareResult {
| ^^^^^^^^^^^^^
warning: trait `Compare` is never used
--> src/lib.rs:16:7
|
16 | trait Compare {
| ^^^^^^^
warning: trait `Filter` is never used
--> src/lib.rs:20:7
|
20 | trait Filter {
| ^^^^^^
warning: trait `Aggregate` is never used
--> src/lib.rs:24:7
|
24 | trait Aggregate {
| ^^^^^^^^^
warning: enum `SortOrder` is never used
--> src/lib.rs:32:6
|
32 | enum SortOrder {
| ^^^^^^^^^
warning: function `checkIFThere` is never used
--> src/lib.rs:58:4
|
58 | fn checkIFThere(test:String,word:&str)->bool
| ^^^^^^^^^^^^
warning: function `process_items` is never used
--> src/lib.rs:103:4
|
103 | fn process_items<T>(items: Vec<T>, query: &str, order: SortOrder) -> (Vec<T>, T::Output)
| ^^^^^^^^^^^^^
warning: function `checkIFThere` should have a snake case name
--> src/lib.rs:58:4
|
58 | fn checkIFThere(test:String,word:&str)->bool
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `check_ifthere`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `vecText` should have a snake case name
--> src/lib.rs:60:9
|
60 | let vecText:Vec<char> = test.chars().collect();
| ^^^^^^^ help: convert the identifier to snake case: `vec_text`
warning: variable `vecWord` should have a snake case name
--> src/lib.rs:61:9
|
61 | let vecWord:Vec<char> = word.chars().collect();
| ^^^^^^^ help: convert the identifier to snake case: `vec_word`
warning: variable `countOfBooks` should have a snake case name
--> src/lib.rs:112:13
|
112 | let countOfBooks = books.len();
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `count_of_books`
warning: variable `aggregateValue` should have a snake case name
--> src/lib.rs:128:16
|
128 | let aggregateValue = T::aggregate(&books);
| ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `aggregate_value`
warning: `solution` (lib) generated 13 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 `checkIFThere` should have a snake case name
--> tests/../src/lib.rs:58:4
|
58 | fn checkIFThere(test:String,word:&str)->bool
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `check_ifthere`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `vecText` should have a snake case name
--> tests/../src/lib.rs:60:9
|
60 | let vecText:Vec<char> = test.chars().collect();
| ^^^^^^^ help: convert the identifier to snake case: `vec_text`
warning: variable `vecWord` should have a snake case name
--> tests/../src/lib.rs:61:9
|
61 | let vecWord:Vec<char> = word.chars().collect();
| ^^^^^^^ help: convert the identifier to snake case: `vec_word`
warning: variable `countOfBooks` should have a snake case name
--> tests/../src/lib.rs:112:13
|
112 | let countOfBooks = books.len();
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `count_of_books`
warning: variable `aggregateValue` should have a snake case name
--> tests/../src/lib.rs:128:16
|
128 | let aggregateValue = T::aggregate(&books);
| ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `aggregate_value`
warning: `solution` (test "solution_test") generated 5 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 10.27s
Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)
running 4 tests
test solution_test::test_aggregate ... ok
test solution_test::test_compare ... ok
test solution_test::test_matches ... ok
test solution_test::test_process ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
