0

I am a newcomer to the huge world of Rust. I have been learning it for a week and got some concept going, however something is a bit wrong with my classic implementation of singly-linked list and it is connected with borrowing and my lack of understanding of lifetimes. Here is the code:

use std::fmt::Display;

#[derive(Debug)]
struct Node<T> {
    payload: T,
    next: Option<Box<Node<T>>>
}

impl<T> Node<T>
    where T: Display + PartialEq {
    fn new(payload: T, next: Option<Box<Node<T>>>) -> Option<Box<Node<T>>> {
        Some(Box::new(Node {
            payload,
            next
        }))
    }

    fn print_nodes(&mut self) {
        let this = self;
        loop {
            match this.next {
                Some(_) => {
                    print!("{} -> ", &this.payload);
                }
                None => {
                    print!("{}", &this.payload);
                    break;
                }
            }
            this = &mut this.next.unwrap();
        }
    }
}

fn main() {
    let a = Node::new(String::from("hello"), None);
    let b = Node::new(String::from("hey"), a);
    let mut d = b.unwrap();
    d.print_nodes();
}

Here is the error I get:

error[E0597]: borrowed value does not live long enough
  --> main.rs:31:43
   |
31 |             this = &mut this.next.unwrap();
   |                         ------------------^ temporary value dropped here while still borrowed
   |                         |
   |                         temporary value created here
32 |         }
33 |     }
   |     - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

error[E0507]: cannot move out of borrowed content
  --> main.rs:31:25
   |
31 |             this = &mut this.next.unwrap();
   |                         ^^^^ cannot move out of borrowed content

error[E0384]: cannot assign twice to immutable variable `this`
  --> main.rs:31:13
   |
20 |         let this = self;
   |             ---- first assignment to `this`
...
31 |             this = &mut this.next.unwrap();
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

I would be grateful if somebody could explain my mistake and recommend something to fix this.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Emulebest
  • 23
  • 3
  • Note: did you really mean lifecyles, or did you mean lifetimes? – Matthieu M. Jan 18 '18 at 14:31
  • You don't need to borrow mutably to *print*; is printing really all you want to do, or is it a placeholder for a function where you *would* like to mutate things? – Matthieu M. Jan 18 '18 at 14:39
  • I wonder why everybody wants to write a linked-list in Rust. IMHO, this is not a good point to start. – Boiethios Jan 18 '18 at 14:43
  • I want to implement my own print that would be able to leave me the context of variable 'd' – Emulebest Jan 18 '18 at 14:46
  • 4
    Obligatory link: [Learning Rust With Entirely Too Many Linked Lists](http://cglab.ca/~abeinges/blah/too-many-lists/book/). – Joe Clay Jan 18 '18 at 15:06
  • Borrowing mutably for printing is just for learning purpose – Emulebest Jan 18 '18 at 15:21
  • Follow the first duplicate, then the second, and you will end up [with code like this](https://play.rust-lang.org/?gist=b3b49e5a95ae47d7dc8e37b69867688b&version=stable). – Shepmaster Jan 18 '18 at 15:44
  • @boie Why not? Sounds like a good way to study how lifetimes and borrowing work using a toy example. – CodesInChaos Jan 18 '18 at 17:13

0 Answers0