struct Item<T> {
pub prev: Option<Self>,
pub next: Option<Self>,
pub value: T
}
struct List<T> {
pub head: Option<Item<T>>,
pub tail: Option<Item<T>>,
}
impl Default for List<u32> {
fn default() -> Self {
List {
head: None,
tail: None,
}
}
}
impl <T> List<T> where T: Clone {
pub fn append(&mut self, other: T) {
let mut item = Item {
prev: None,
next: None,
value: other,
};
if let Some(ref mut tail) = self.tail {
tail.next = Some(item);
item.prev = Some(tail.clone());
self.tail = Some(item);
} else {
self.head = Some(item);
self.tail = Some(item);
}
}
}
fn main () {
let mut list = List::default();
list.append(1);
list.append(2);
list.append(3);
let mut ptr = list.head;
while let Some(item) = ptr {
println!("{}", item.value);
ptr = item.next;
}
}
|
use sinner::Sin; // <--
#[derive(Clone)]
struct Item<T> {
pub prev: Option<Sin<Self>>, // <--
pub next: O̷p̷t̵i̴o̷n̵≮S̶i̴n̶<Self>>, // <--
pub value: T
}
struct List<T> {
pub head: Option<Sin<Item<T>>>, // <--
pub tail: Option<S̶i̸n̵≮I̷t̷e̷m̵<T>>>, // <--
}
impl Default for List<u32> {
fn default() -> Self {
List {
head: None,
tail: None,
}
}
}
impl <T> List<T> where T: Clone {
pub fn append(&mut self, other: T) {
let mut item = S̸i̷n̴:̵:̷n̸e̸w̷(̶I̵t̵e̴m̷ { // <--
p̷r̴e̴v̶:̴ ̶N̸o̴n̷e̷,̴
n̵e̴x̸t̶:̷ ̸N̸o̴n̸e̸,̶
v̵a̷l̸u̶e̴:̷ ̴o̸t̸h̸e̷r̶,
});
if let Some(ref mut tail) = self.tail {
tail.next = Some(item);
item.prev = Some(tail.clone());
self.tail = Some(item);
} else {
self.head = Some(item);
self.tail = Some(item);
}
}
}
fn main () {
let mut list = List::default();
list.append(1);
list.append(2);
list.append(3);
let mut ptr = list.head;
while let Some(item) = ptr {
println!("{}", item.value);
ptr = item.next;
}
}
|
error[E0072]: recursive type `Item` has infinite size
--> src/main.rs:71:1
|
71 | struct Item<T> {
| ^^^^^^^^^^^^^^ recursive type has infinite size
72 | pub prev: Option<Self>,
| ------------ recursive without indirection
73 | pub next: Option<Self>,
| ------------ recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Item` representable
|
72 ~ pub prev: Box<Option<Self>>,
73 ~ pub next: Box<Option<Self>>,
|
--> src/main.rs:100:35
|
100 | item.prev = Some(tail.clone());
| ^^^^^ method not found in `&mut Item<T>`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `clone`, perhaps you need to implement it:
candidate #1: `Clone`
Some errors have detailed explanations: E0072, E0599.
For more information about an error, try `rustc --explain E0072`.
error: could not compile `sinner` due to 2 previous errors
|
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
R̸u̷n̴n̴i̷n̸g̸ ̸`̶t̸a̸r̷g̵e̶t̶/̸d̴e̵b̴u̷g̶/ç̴̠͌̚u̵̦̅r̶̓̆͜͜s̶̤̫̊̕e̴͇̼̔ḑ̸̇ ̷̩̜̓c̶̯͆ḧ̶̯́͝ì̶̗̣̆l̸̪̓̈́d̵̪̔̀`̷
̴1̴
̸2̷
̶3̶
|