Решение на упр.05 задача 1 от Божидар Виденов
Към профила на Божидар Виденов
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::{collections::HashMap, hash::Hash};
pub trait MyFromIterator<A> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = A>;
}
impl<T> MyFromIterator<T> for Vec<T> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
{
let mut final_collection: Vec<T> = Vec::new();
iter.for_each(|item| {
final_collection.push(item);
});
final_collection
}
}
impl<T> MyFromIterator<T> for String
where T: Into<char> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
{
let mut str: String = String::new();
iter.for_each(|ch| {
str.push(ch.into());
});
str
}
}
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 final_map: HashMap<K, V> = HashMap::new();
iter.for_each(|(key, value)| {
final_map.insert(key, value);
});
final_map
}
}
fn my_collect<I, C, T>(iter: I) -> C
where
C: MyFromIterator<T>,
I: Iterator<Item = T>
{
C::my_from_iter(iter)
}
Лог от изпълнението
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 futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling pin-utils v0.1.0
Compiling syn v2.0.110
Compiling memchr v2.7.6
Compiling slab v0.4.11
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251113-1757769-1lye5r1/solution)
warning: function `my_collect` is never used
--> src/lib.rs:57:4
|
57 | fn my_collect<I, C, T>(iter: I) -> C
| ^^^^^^^^^^
|
= 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
Finished `test` profile [unoptimized + debuginfo] target(s) in 14.46s
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
История (2 версии и 0 коментара)
Божидар качи решение на 06.11.2025 19:26 (преди 29 дена)
use std::{collections::HashMap, hash::Hash};
pub trait MyFromIterator<A> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = A>;
}
impl<T> MyFromIterator<T> for Vec<T> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
{
let mut final_collection: Vec<T> = Vec::new();
iter.for_each(|item| {
final_collection.push(item);
});
final_collection
}
}
-impl MyFromIterator<char> for String {
+impl<T> MyFromIterator<T> for String
+where T: Into<char> {
fn my_from_iter<I>(iter: I) -> Self
where
- I: Iterator<Item = char>,
+ I: Iterator<Item = T>,
{
let mut str: String = String::new();
iter.for_each(|ch| {
- str.push(ch);
- });
-
- str
- }
-}
-
-impl MyFromIterator<u8> for String {
- fn my_from_iter<I>(iter: I) -> Self
- where
- I: Iterator<Item = u8>,
- {
- let mut str: String = String::new();
-
- iter.for_each(|byte| {
- str.push(byte as char);
+ str.push(ch.into());
});
str
}
}
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 final_map: HashMap<K, V> = HashMap::new();
iter.for_each(|(key, value)| {
final_map.insert(key, value);
});
final_map
}
}
fn my_collect<I, C, T>(iter: I) -> C
where
C: MyFromIterator<T>,
I: Iterator<Item = T>
{
C::my_from_iter(iter)
}
