You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not skilled enough with Rust yet to understand the nature of the errors, but the code provided in the first-drop.html is no longer accurate.
I've followed the tutorial as closely as possible and checked as much as possible
I'm using rustc version 1.74.0
Here is my main.rs
use std::mem;pubstructList{head:Link,}enumLink{Empty,More(Box<Node>),}structNode{elem:i32,next:Link,}implList{pubfnnew() -> Self{List{head:Link::Empty}}pubfnpush(&mutself,elem:i32){let new_node = Box::new(Node{elem: elem,next: mem::replace(&mutself.head,Link::Empty)});self.head = Link::More(new_node);}pubfnpop(&mutself) -> Option<i32>{match mem::replace(&mutself.head,Link::Empty){Link::Empty => None,Link::More(node) => {self.head = node.next;Some(node.elem)}}}}// impl Drop for List {// fn drop(&mut self) {// println!("Dropping List");// }// }implDropforList{fndrop(&mutself){letmut cur_link = mem::replace(&mutself.head,Link::Empty);// `while let` == "do this thing until this pattern doesn't match"whileletLink::More(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.next,Link::Empty);// boxed_node goes out of scope and gets dropped here;// but its Node's `next` field has been set to Link::Empty// so no unbounded recursion occurs.}}}implDropforLink{fndrop(&mutself){match*self{Link::Empty => {}Link::More(refmut boxed_node) => {
boxed_node.drop();}}}}implDropforBox<Node>{fndrop(&mutself){self.ptr.drop();deallocate(self.ptr);}}implDropforNode{fndrop(&mutself){println!("Dropping Node");self.next.drop();}}fnmain(){println!("Hello, world!");}#[cfg(test)]mod test {usesuper::List;#[test]fnbasics(){letmut list = List::new();assert_eq!(list.pop(), None);
list.push(1);
list.push(2);
list.push(3);assert_eq!(list.pop(), Some(3));assert_eq!(list.pop(), Some(2));
list.push(4);
list.push(5);assert_eq!(list.pop(), Some(5));assert_eq!(list.pop(), Some(4));assert_eq!(list.pop(), Some(1));assert_eq!(list.pop(), None);}}
Here is the output of the cargo build
➜ toomanylists git:(master) ✗ cargo build
Compiling toomanylists v0.1.0 (/Users/ramseybarghouti/rust-too-many-lists/toomanylists)
error[E0119]: conflicting implementations of trait `Drop`fortype`Box<Node>`
--> src/main.rs:59:1
|
59 | impl Drop for Box<Node> {
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `alloc`:
- impl<T, A> Drop for Box<T, A>
where A: Allocator, T: ?Sized;
error[E0120]: the `Drop` trait may only be implemented forlocal structs, enums, and unions
--> src/main.rs:59:15
|
59 | impl Drop for Box<Node> {
| ^^^^^^^^^ must be a struct, enum, or union in the current crate
error[E0425]: cannot find function`deallocate`in this scope
--> src/main.rs:62:9
|
62 | deallocate(self.ptr);| ^^^^^^^^^^ not found in this scope
Some errors have detailed explanations: E0119, E0120, E0425.
For more information about an error, try `rustc --explain E0119`.
I think that what's intended in this tutorial is to only implement the Drop trait on the List Struct and not implement the Drop trait on List, Box<Node> and Node types, but I'm not sure. If that's the case I think this page simply needs some clarification.
The text was updated successfully, but these errors were encountered:
I'm not skilled enough with Rust yet to understand the nature of the errors, but the code provided in the
first-drop.html
is no longer accurate.I've followed the tutorial as closely as possible and checked as much as possible
I'm using rustc version 1.74.0
Here is my main.rs
Here is the output of the
cargo build
I think that what's intended in this tutorial is to only implement the
Drop
trait on theList
Struct and not implement theDrop
trait onList
,Box<Node>
andNode
types, but I'm not sure. If that's the case I think this page simply needs some clarification.The text was updated successfully, but these errors were encountered: