rust - "does not live long enough" error when iterating through a linked list -


I'm trying to iterate through a linked list and a list to iterate.

  enum catalog & lt; T & gt; {Contract (T, box & lieutent; list & lt; T & gt; & gt;), zero,} Straight list earrings & lt; 'A, T:' a & gt; {Tax: & amp; A list & lt; T & gt; } Impl & lt; 'A, t & gt; Iterator & LT; T & gt; For Lists IITs & lt; 'A, t & gt; {FN Next (& self) - & gt; Options & lt; T & gt; {Match self-cure {& amp; Bracket (val, next) = & gt; {Self.cur = & amp; next; Some (val)}, and neil = & gt; None}}}  

but I get the following error:

  error: `next 'does not stay too long   
  and opposition (val, next) => in the hand of your match {Self.cur = & amp; next; Some (val)},  

are trying to immerse a list item in and cons (wall, next) to move It is not allowed in the back of an attempt to valid and next lending indicator but if this happens, then val and next Local time will be with the life of a short time, the error message you are seeing is the reason for this: Next will be a local box which will be left Is. This type of and is a reference with a lifetime parameter giving reference to the lifetime of this local box.

It calls to use the ref and box pattern:

  & Amp ; Cons (referee val, next to refit the box) = & gt; {Self.cur = next; Some (* val)},  

Now, val type & amp; T and next type & amp; The list is where both of your list data structure contains references and nothing turns out. Note also that, some (* val) should be T Copy :

  impl & lt; 'A, t: copy & gt; Iterator & LT; T & gt; For Lists IITs & lt; 'A, t & gt; {...  

but copy requires a more general way clone to be used like this:

  impl & lt; 'A, t: clone & gt; Iterator & LT; T & gt; For Lists IITs & lt; 'A, t & gt; {... and the opposition (referee val, next to refit the box) => {Self.cur = next; Some (* val) .clone ())},  

This works for more types. And if you do not want to clone prices, then you have been done with vectors and slices, instead of T instead of your Iterator yield & amp; You can create T .


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -