Решение на упр.08 задача 1 от Божидар Виденов

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

Към профила на Божидар Виденов

Резултати

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

Код

use std::rc::Rc;
struct List<T> {
head: Link<T>,
}
type Link<T> = Option<Rc<Node<T>>>;
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<T> List<T> {
fn new() -> Self {
List { head: None }
}
fn head(&self) -> Option<&T> {
match &self.head {
None => None,
Some(link) => Some(&link.elem),
}
}
fn tail(&self) -> List<T> {
match &self.head {
Some(head) => List {
head: head.next.clone(),
},
None => List { head: None },
}
}
fn prepend(&self, value: T) -> List<T> {
let new_head: Link<T> = Some(Rc::new(Node {
elem: value,
next: self.head.clone(),
}));
List { head: new_head }
}
fn iter(&self) -> Iter<'_, T> {
Iter {
curr_el: &self.head,
}
}
}
struct Iter<'a, T> {
curr_el: &'a Link<T>,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
match self.curr_el {
None => None,
Some(node) => {
self.curr_el = &node.next;
Some(&node.elem)
}
}
}
}

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

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 futures-task v0.3.31
   Compiling syn v2.0.111
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251211-1757769-goo77b/solution)
warning: struct `List` is never constructed
 --> src/lib.rs:3:8
  |
3 | struct List<T> {
  |        ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Link` is never used
 --> src/lib.rs:7:6
  |
7 | type Link<T> = Option<Rc<Node<T>>>;
  |      ^^^^

warning: struct `Node` is never constructed
 --> src/lib.rs:9:8
  |
9 | struct Node<T> {
  |        ^^^^

warning: associated items `new`, `head`, `tail`, `prepend`, and `iter` are never used
  --> src/lib.rs:15:8
   |
14 | impl<T> List<T> {
   | --------------- associated items in this implementation
15 |     fn new() -> Self {
   |        ^^^
...
19 |     fn head(&self) -> Option<&T> {
   |        ^^^^
...
26 |     fn tail(&self) -> List<T> {
   |        ^^^^
...
35 |     fn prepend(&self, value: T) -> List<T> {
   |        ^^^^^^^
...
44 |     fn iter(&self) -> Iter<'_, T> {
   |        ^^^^

warning: struct `Iter` is never constructed
  --> src/lib.rs:51:8
   |
51 | struct Iter<'a, T> {
   |        ^^^^

warning: `solution` (lib) generated 5 warnings
   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 8.36s
     Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)

running 2 tests
test solution_test::test_list ... ok
test solution_test::test_iterator ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

Божидар качи първо решение на 27.11.2025 19:18 (преди 2 месеца)