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 moveIt is not allowed in the back of an attempt to valid
andnext
lending indicator but if this happens, thenval
andnext
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 ofand
is a reference with a lifetime parameter giving reference to the lifetime of this local box.
It calls to use the
ref
andbox
pattern:& Amp ; Cons (referee val, next to refit the box) = & gt; {Self.cur = next; Some (* val)},
Now,
val
type& amp; T
andnext
type& amp; The list is
where both of your list data structure contains references and nothing turns out. Note also that,some (* val)
should beT
Copy
:impl & lt; 'A, t: copy & gt; Iterator & LT; T & gt; For Lists IITs & lt; 'A, t & gt; {...
but
copy
requires a more general wayclone
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
Post a Comment