Решение на упр.05 задача 1 от Деян Делчев

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

Към профила на Деян Делчев

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 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 res = Vec::new();
for el in iter {
res.push(el);
}
res
}
}
impl MyFromIterator<char> for String {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = char>,
{
let mut res = String::new();
for el in iter {
res.push(el);
}
res
}
}
impl<K: Hash+ Eq, V> MyFromIterator<(K, V)> for HashMap<K, V> {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = (K, V)>,
{
let mut res = HashMap::new();
for (k, v) in iter {
res.insert(k, v);
}
res
}
}

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

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-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling syn v2.0.110
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251113-1757769-h134b6/solution)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
error[E0425]: cannot find function `my_collect` in this scope
  --> tests/solution_test.rs:10:26
   |
10 |     let bytes: Vec<u8> = my_collect("hello".bytes());
   |                          ^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `my_collect` in this scope
  --> tests/solution_test.rs:13:28
   |
13 |     let chars: Vec<char> = my_collect("здравей".chars());
   |                            ^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `my_collect` in this scope
  --> tests/solution_test.rs:16:47
   |
16 |     let index_to_char: HashMap<usize, char> = my_collect("здр".char_indices());
   |                                               ^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `my_collect` in this scope
  --> tests/solution_test.rs:23:26
   |
23 |     let digits: String = my_collect("1a2b3c4d".chars().filter(|c| c.is_digit(10)));
   |                          ^^^^^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `solution` (test "solution_test") due to 4 previous errors

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

Деян качи първо решение на 10.11.2025 15:45 (преди 26 дена)

Сега видях че не съм копирал my_collect фунцията:

use std::{collections::HashMap, hash::Hash}; pub trait MyFromIterator { fn my_from_iter(iter: I) -> Self where I: Iterator; }

impl MyFromIterator for Vec { fn my_from_iter(iter: I) -> Self where I: Iterator, { let mut res = Vec::new();

    for el in iter {
        res.push(el);
    }

    res
}

}

impl MyFromIterator for String { fn my_from_iter(iter: I) -> Self where I: Iterator, { let mut res = String::new();

    for el in iter {
        res.push(el);
    }

    res
}

}

impl<K: Hash+ Eq, V> MyFromIterator<(K, V)> for HashMap<K, V> { fn my_from_iter(iter: I) -> Self where I: Iterator, { let mut res = HashMap::new();

    for (k, v) in iter {
        res.insert(k, v);
    }

    res
}

}

fn my_collect<I: Iterator, C: MyFromIterator<I::Item>>(iter: I) -> C { MyFromIterator::my_from_iter(iter) }