How can I correct “cannot infer an appropriate lifetime” without editing function signature?

前端 未结 1 895
后悔当初
后悔当初 2021-01-25 07:11

Background: I\'m creating an iterator that returns a reference to a slice &[T], but the data vector needs to remain immutable. The iterator cannot

相关标签:
1条回答
  • 2021-01-25 08:00

    Rust prevents you from having more than one alias to an object if one of them is a mutable alias.

    Here, Windower::out_data is a mutable alias to some slice, and you're trying to return an immutable alias to the same data from your next method. In order for this to be safe, Rust must prevent you from being able to use Windower::out_data for as long as the slice returned by next is in scope. This means that the signature fn next(&'b mut self) -> Option<Self::Item> is indeed required, which means you simply cannot implement Iterator with your current implementation.

    0 讨论(0)
提交回复
热议问题