Решение на упр.05 задача 1 от Борис Великов
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::HashMap;
use std::hash::Hash;
pub trait MyFromIterator<A> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = A>;
}
// итератор по T -> Vec<T>
impl<T> MyFromIterator<T> for Vec<T> {
fn my_from_iter<I>(iter : I) -> Self
where I: Iterator<Item = T> {
let mut v = Vec::new();
for item in iter {
v.push(item);
}
v
}
}
// итератор по char -> String
impl MyFromIterator<char> for String {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = char>,
{
let mut s = String::new();
for c in iter {
s.push(c);
}
s
}
}
// интератор по (K, V) -> HashMap<K, V>
impl<K,V> MyFromIterator<(K,V)> for HashMap<K, V>
where K: Eq + Hash,
{
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = (K,V)>,
{
let mut map = HashMap::new();
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
fn my_collect<I, C>(iter: I) -> C
where
I: Iterator,
C: MyFromIterator<I::Item>,
{
C::my_from_iter(iter)
}
fn main(){
let bytes: Vec<u8> = my_collect("hello".bytes());
let chars: Vec<char> = my_collect("hello".chars());
let index_to_char: HashMap<usize, char> = my_collect("hello".char_indices());
let digits: String = my_collect("1a2b3c4d".chars().filter(|c| c.is_digit(10)));
}
Лог от изпълнението
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.42
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling pin-project-lite v0.2.16
Compiling slab v0.4.11
Compiling syn v2.0.110
Compiling pin-utils v0.1.0
Compiling memchr v2.7.6
Compiling futures-io v0.3.31
Compiling futures-task v0.3.31
Compiling solution v0.1.0 (/tmp/d20251113-1757769-93iti3/solution)
warning: unused variable: `bytes`
--> src/lib.rs:64:9
|
64 | let bytes: Vec<u8> = my_collect("hello".bytes());
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_bytes`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `chars`
--> src/lib.rs:65:9
|
65 | let chars: Vec<char> = my_collect("hello".chars());
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_chars`
warning: unused variable: `index_to_char`
--> src/lib.rs:66:9
|
66 | let index_to_char: HashMap<usize, char> = my_collect("hello".char_indices());
| ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_index_to_char`
warning: unused variable: `digits`
--> src/lib.rs:67:9
|
67 | let digits: String = my_collect("1a2b3c4d".chars().filter(|c| c.is_digit(10)));
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_digits`
warning: function `my_collect` is never used
--> src/lib.rs:55:4
|
55 | fn my_collect<I, C>(iter: I) -> C
| ^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `main` is never used
--> src/lib.rs:63:4
|
63 | fn main(){
| ^^^^
warning: `solution` (lib) generated 6 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: unused variable: `bytes`
--> tests/../src/lib.rs:64:9
|
64 | let bytes: Vec<u8> = my_collect("hello".bytes());
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_bytes`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `chars`
--> tests/../src/lib.rs:65:9
|
65 | let chars: Vec<char> = my_collect("hello".chars());
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_chars`
warning: unused variable: `index_to_char`
--> tests/../src/lib.rs:66:9
|
66 | let index_to_char: HashMap<usize, char> = my_collect("hello".char_indices());
| ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_index_to_char`
warning: unused variable: `digits`
--> tests/../src/lib.rs:67:9
|
67 | let digits: String = my_collect("1a2b3c4d".chars().filter(|c| c.is_digit(10)));
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_digits`
warning: function `main` is never used
--> tests/../src/lib.rs:63:4
|
63 | fn main(){
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (test "solution_test") generated 5 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.67s
Running tests/solution_test.rs (target/debug/deps/solution_test-f75e629a1d90e17c)
running 1 test
test solution_test::test_basic ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
