Решение на упр.08 задача 1 от Владимир Великов

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

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

Резултати

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

Код

use std::rc::Rc;
use std::option::Option;
type Link<T> = Option<Rc<Node<T>>>;
struct List<T> {
head: Link<T>,
}
struct Iter<'a, T> {
next: Option<&'a 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> {
self.head.as_ref().map(|node| &node.elem)
}
fn tail(&self) -> List<T> {
let new_head = self.head.as_ref().and_then(|node| node.next.clone());
List { head: new_head }
}
fn prepend(&self, value: T) -> List<T> {
let new_node = Rc::new(Node {
elem: value,
next: self.head.clone(),
});
List { head: Some(new_node) }
}
fn iter(&self) -> Iter<'_, T> {
let next_node_ref = self.head.as_ref().map(|node_rc| &**node_rc);
Iter { next: next_node_ref }
}
}
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut current_link = self.head.take();
while let Some(rc_node) = current_link {
if let Ok(mut node) = Rc::try_unwrap(rc_node) {
current_link = node.next.take();
} else {
break;
}
}
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.next.map(|node| {
self.next = node.next.as_ref().map(|next_rc| &**next_rc);
&node.elem
})
}
}

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

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-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling syn v2.0.111
   Compiling memchr v2.7.6
   Compiling futures-io v0.3.31
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251211-1757769-9zygy4/solution)
warning: type alias `Link` is never used
 --> src/lib.rs:5:6
  |
5 | type Link<T> = Option<Rc<Node<T>>>;
  |      ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

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

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

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

warning: associated items `new`, `head`, `tail`, `prepend`, and `iter` are never used
  --> src/lib.rs:20:8
   |
19 | impl<T> List<T> {
   | --------------- associated items in this implementation
20 |     fn new() -> Self {
   |        ^^^
...
24 |     fn head(&self) -> Option<&T> {
   |        ^^^^
...
28 |     fn tail(&self) -> List<T> {
   |        ^^^^
...
34 |     fn prepend(&self, value: T) -> List<T> {
   |        ^^^^^^^
...
43 |     fn iter(&self) -> Iter<'_, 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.44s
     Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)

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

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

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

Владимир качи първо решение на 02.12.2025 19:50 (преди около 2 месеца)