# How to resolve creates a temporary variable which is freed while still in use?

**URL:** <https://users.rust-lang.org/t/how-to-resolve-creates-a-temporary-variable-which-is-freed-while-still-in-use/51244>\
**Category:** help\
**Created:** [November 9, 2020, 2:45pm UTC](https://users.rust-lang.org/t/how-to-resolve-creates-a-temporary-variable-which-is-freed-while-still-in-use/51244 "2020-11-09T14:45:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawan9489](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pawan9489/32/20442_2.png) [@pawan9489](https://users.rust-lang.org/u/pawan9489)\
**Post date:** [November 9, 2020, 2:45pm UTC](https://users.rust-lang.org/t/how-to-resolve-creates-a-temporary-variable-which-is-freed-while-still-in-use/51244/1 "2020-11-09T14:45:01Z")

</div>

I am trying to implement a `Debug` for my custom `List<T>` using a `while` loop but stuck at taking a reference of a pattern matched RefCell.

```rust
use std::rc::Rc;
use std::cell::RefCell;

type Link<T> = Option<Rc<RefCell<Node<T>>>>;
struct Node<T> {val: T, next: Link<T>}
struct List<T> {head: Link<T>, tail: Link<T>, len: usize}

use std::fmt;
impl<T: fmt::Debug> fmt::Debug for List<T> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let mut temp = &self.head;
    while let Some(r) = temp {
      write!(f, "{:?} -> ", r.borrow().val);
      temp = &r.borrow().next; // ERROR
      // temp = unsafe {& (*r.as_ptr()).next };
    }
    write!(f, "[]")
  }
}

```

Is there an Elegant, Idiomatic & Safe Rust that can get around this problem?  
I don't want to implement Iterator for my list.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [November 9, 2020, 3:00pm UTC](https://users.rust-lang.org/t/how-to-resolve-creates-a-temporary-variable-which-is-freed-while-still-in-use/51244/2 "2020-11-09T15:00:42Z")

</div>

You need to clone the `Rc`.

```rust
impl<T: fmt::Debug> fmt::Debug for List<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut temp = self.head.clone();
        while let Some(r) = temp {
            write!(f, "{:?} -> ", r.borrow().val);
            temp = r.borrow().next.clone();
        }
        write!(f, "[]")
    }
}

```

[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6cbe1ea4487864114fff0ab54c6aed6d)

The reason for this is that `borrow()` returns a guard with a destructor, and you can only access references into the `RefCell` while that guard is still active. So without cloning an `Rc`, you would need to keep all the guards from each iteration around, but the loop destroys the previous guard when advancing to the next iteration.

An `Rc` clone lets you access the next node without going through the guard, hence sidestepping the issue.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 7, 2021, 3:00pm UTC](https://users.rust-lang.org/t/how-to-resolve-creates-a-temporary-variable-which-is-freed-while-still-in-use/51244/3 "2021-02-07T15:00:45Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
