Решение на упр.05 задача 1 от Милен Хаджиев

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

Към профила на Милен Хаджиев

Резултати

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

Код

use std::collections::{HashMap, hash_map::RandomState};
use std::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 v = match iter.size_hint() {
(_, Some(upper)) => Vec::with_capacity(upper),
_ => Vec::new(),
};
for item in iter {
v.push(item);
}
v
}
}
impl MyFromIterator<char> for String {
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = char>,
{
let mut s = String::new();
for ch in iter {
s.push(ch);
}
s
}
}
impl<K, V, S> MyFromIterator<(K, V)> for HashMap<K, V, S>
where
K: Eq + Hash,
S: std::hash::BuildHasher + Default,
{
fn my_from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = (K, V)>,
{
let mut map = match iter.size_hint() {
(_, Some(upper)) => HashMap::with_capacity_and_hasher(upper, S::default()),
_ => HashMap::with_hasher(S::default()),
};
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
pub fn my_collect<I, C>(iter: I) -> C
where
I: Iterator,
C: MyFromIterator<I::Item>,
{
C::my_from_iter(iter)
}

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

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-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling slab v0.4.11
   Compiling syn v2.0.110
   Compiling memchr v2.7.6
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251113-1757769-mp9f00/solution)
warning: unused import: `hash_map::RandomState`
 --> src/lib.rs:1:33
  |
1 | use std::collections::{HashMap, hash_map::RandomState};
  |                                 ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `solution` (lib) generated 1 warning (run `cargo fix --lib -p solution` to apply 1 suggestion)
   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 import: `hash_map::RandomState`
 --> tests/../src/lib.rs:1:33
  |
1 | use std::collections::{HashMap, hash_map::RandomState};
  |                                 ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `solution` (test "solution_test") generated 1 warning (run `cargo fix --test "solution_test"` to apply 1 suggestion)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 10.05s
     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 коментара)

Милен качи първо решение на 11.11.2025 20:43 (преди 24 дена)