Store a reference

Hello,

I am coding a small program where I traverse a tree structure using a visitor. During the visit I want to store a reference to some node in my visitor struct.
My code currently looks like this :

struct Visitor<'a> {
    saved_node: &'a Node,
}

impl Visit<Node> for Visitor {
    fn visit(&mut self, node: &Node) {
        self.node = node;
    }
}

However the lifetime checker is screaming and I can't get it to work. Is it possible to do it using somehow explicit lifetimes ?

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
  --> src\main.rs:12:28
   |
12 |         self.saved_node= node;
   |                            ^^^^^
   |
   = note: ...the reference is valid for the static lifetime...
note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the method body at 11:5
  --> src\main.rs:11:5
   |
11 | /     fn visit(&mut self, node: &Node) {
12 | |         self.saved_node = node;
13 | |     }
   | |_____^

Thank you for any help you could give me !

The problem is that you are leaving out a lot of lifetime annotations and the compiler doesn't understand how long these references need to live.

I got this to compile

struct Node {}
struct Visitor<'a> {
    saved_node: &'a Node,
}

trait Visit<'a> {
    fn visit(&mut self, node: &'a Node);
}

impl<'a> Visit<'a> for Visitor<'a> {
    fn visit(&mut self, node: &'a Node) {
        self.saved_node = node;
    }
}

also you can simplify it a bit by getting ride of the trait if you don't need it.

struct Node {}
struct Visitor<'a> {
    saved_node: &'a Node,
}

impl<'a> Visitor<'a> {
    fn visit(&mut self, node: &'a Node) {
        self.saved_node = node;
    }
}

Hi, you are currently using quote blocks for code, but you should be using code blocks instead.

```
// this is a code block
```

Per @alice's comment, please read Forum Code Formatting and Syntax Highlighting and edit your initial post to improve its readability by the usual visitors to this forum. Thanks. :clap: