Решение на упр.05 задача 1 от Станислав Стаматов

Обратно към всички решения

Към профила на Станислав Стаматов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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>(mut iter: I) -> Self
where
I: Iterator<Item = T>,
{
let mut result: Vec<T> = Vec::new();
while let Some(curr) = iter.next() {
result.push(curr);
}
result
}
}
// итератор по char -> String
impl MyFromIterator<char> for String {
fn my_from_iter<I>(mut iter: I) -> Self
where
I: Iterator<Item = char>,
{
let mut result: String = String::new();
while let Some(curr) = iter.next() {
result.push(curr);
}
result
}
}
// интератор по (K, V) -> HashMap<K, V>
impl<K: Eq + Hash, V> MyFromIterator<(K, V)> for HashMap<K, V> {
fn my_from_iter<I>(mut iter: I) -> Self
where
I: Iterator<Item = (K, V)>,
{
let mut result: HashMap<K, V> = HashMap::new();
while let Some((key, value)) = iter.next() {
result.insert(key, value);
}
result
}
}
use std::{char, collections::HashMap, hash::Hash};
fn my_collect<I: Iterator, C: MyFromIterator<I::Item>>(iter: I) -> C {
MyFromIterator::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)));
println!("{:?}", bytes);
println!("{:?}", chars);
println!("{:?}", index_to_char);
println!("{:?}", digits);
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.42
   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 futures-task v0.3.31
   Compiling syn v2.0.110
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.16
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251113-1757769-alje7d/solution)
warning: function `my_collect` is never used
  --> src/lib.rs:50:4
   |
50 | fn my_collect<I: Iterator, C: MyFromIterator<I::Item>>(iter: I) -> C {
   |    ^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: function `main` is never used
  --> src/lib.rs:54:4
   |
54 | fn main() {
   |    ^^^^

warning: `solution` (lib) generated 2 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:54:4
   |
54 | 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.70s
     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

История (1 версия и 0 коментара)

Станислав качи първо решение на 12.11.2025 10:17 (преди 24 дена)