#Creating a boxed self referential struct, then editing it's data in a loop?

1 messages · Page 1 of 1 (latest)

round tendon
#

https://replit.com/@CrabBucket1/OutgoingElatedSlash#main.rs Trying to do a relatively easy leetcode problem but Im running into trouble with Box pointer (given in the datastructure from leetcode) https://i.imgur.com/NjkM0gK.png

The struct looks liek this

  pub val: i32,
  pub next: Option<Box<ListNode>>
}```

I want to create a Box pointer to the first ListNode like `let result = Box::new(ListNode::new(0))` the inside the loop I want to update the correct value based on the function inputs until both `Option<Box<ListNode>>` are none and I always seem to get borrow issues one or the other depending on different attempts at implementing.

The LeetCode Problem:
https://leetcode.com/problems/add-two-numbers/

Any advice would be appreciated (Also here is a valid solution to the problem that doesn't mutate already allocated nodes: https://levelup.gitconnected.com/leetcode-problems-add-two-numbers-6a4045ce146c
Medium

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their…

CrabBucket1

Ownership frustration

tepid wharf
#

On your match statement you move current1 and current2 and then assign new values to them (variables that are already moved). To avoid this, call current1.take() and current2.take() to take their values and replace them with None.