Решение на упр.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>,
}
struct Iter<'a, T> {
next: Option<&'a Node<T>>,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.next.map(|n| { self.next = n.next.as_deref();
&n.elem
})
}
}
impl<T> List<T> {
fn new() -> Self {
List{head: None}
}
fn head(&self) -> Option<&T> {
self.head.as_ref().map(|n| &n.elem)
}
fn tail(&self) -> List<T> {
List { head: self.head.as_ref().and_then(|n| n.next.clone())}
}
fn prepend(&self, value: T) -> List<T> {
List {
head: Some(Rc::new(Node {
elem: value,
next: self.head.clone(),
})),
}
}
fn iter(&self) -> Iter<'_, T> {
Iter {next: self.head.as_deref()}
}
}
impl<T> List<T> {
fn drop2(mut self){
let mut head = self.head.take();
while let Some(node) = head {
head = Rc::try_unwrap(node).ok().and_then(|n| n.next);
}
}
}
fn main() {
let mut list = List::new();
for i in 0..1_000_000 {
list = list.prepend(i);
}
list.drop2();
}
Лог от изпълнението
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-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-task v0.3.31
Compiling pin-project-lite v0.2.16
Compiling syn v2.0.111
Compiling pin-utils v0.1.0
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling futures-io v0.3.31
Compiling solution v0.1.0 (/tmp/d20251211-1757769-db9n9o/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: struct `Iter` is never constructed
--> src/lib.rs:14:8
|
14 | struct Iter<'a, T> {
| ^^^^
warning: associated items `new`, `head`, `tail`, `prepend`, and `iter` are never used
--> src/lib.rs:29:8
|
28 | impl<T> List<T> {
| --------------- associated items in this implementation
29 | fn new() -> Self {
| ^^^
...
33 | fn head(&self) -> Option<&T> {
| ^^^^
...
37 | fn tail(&self) -> List<T> {
| ^^^^
...
41 | fn prepend(&self, value: T) -> List<T> {
| ^^^^^^^
...
50 | fn iter(&self) -> Iter<'_, T> {
| ^^^^
warning: method `drop2` is never used
--> src/lib.rs:56:8
|
55 | impl<T> List<T> {
| --------------- method in this implementation
56 | fn drop2(mut self){
| ^^^^^
warning: function `main` is never used
--> src/lib.rs:65:4
|
65 | fn main() {
| ^^^^
warning: `solution` (lib) generated 7 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: method `drop2` is never used
--> tests/../src/lib.rs:56:8
|
55 | impl<T> List<T> {
| --------------- method in this implementation
56 | fn drop2(mut self){
| ^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `main` is never used
--> tests/../src/lib.rs:65:4
|
65 | fn main() {
| ^^^^
warning: `solution` (test "solution_test") generated 2 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.54s
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
