Linked Questions
30 questions linked to/from How can I create my own data structure with an iterator that returns mutable references?
11 votes
1 answer
17k views
In Rust, how do I create a mutable iterator? [duplicate]
I'm having difficulty with lifetimes when trying to create a mutable iterator in safe Rust. Here is what I have reduced my problem to: struct DataStruct<T> { inner: Box<[T]>, } pub ...
4 votes
2 answers
1k views
How to implement Iterator yielding mutable references [duplicate]
I am trying to implement a simple lookup iterator: pub struct LookupIterMut<'a, D> { data : &'a mut [D], indices : &'a [usize], i: usize } impl<'a, D> Iterator for ...
5 votes
1 answer
1k views
How do I implement a container with support for a mutable iterator? [duplicate]
I want to design a toy container class with support for mutable iterators, but I'm having trouble sorting out the lifetimes of the iterator and its reference to the container. I've tried to create a ...
2 votes
1 answer
814 views
Can't use mutable slice where immutable slice is fine [duplicate]
I'd like to create a struct that holds a slice, and can return references to items in that slice. So far, I've been able to do this: pub struct Stride<'a> { items: &'a [f32], } impl<...
1 vote
0 answers
1k views
Rust: return reference to member variable in a function in an `impl` [duplicate]
How do I scope the lifetime for a function that returns a reference to a member variable? The reference will be valid as long as the struct is alive. Here is an example where one might want this: ...
4 votes
0 answers
438 views
How to create an iterator that allows mapping indices to mutable items in a slice [duplicate]
I have a slice of items and a slice of indices into the first slice, essentially giving me a sub-group of items that I want to modify. To iterate over the items and manipulate them I can create the ...
0 votes
1 answer
289 views
Mutable iterator [duplicate]
I am trying to write a mutable iterator over a vector, but I am unable to figure out what the compiler is trying to tell me. Here is my implementation struct IterMut<'a> { vec: &'a mut ...
0 votes
1 answer
190 views
lifetime / borrow of nested structs error [duplicate]
I'm having a lifetime issue when implementing an iterator on custom struct containing borrows of vecs. I've been trying different solutions but can't fix it by myself (I'm still a beginner) and I want ...
2 votes
0 answers
169 views
Why doesn't Rust let you use the innermost lifetime of nested mutable references? [duplicate]
I have encountered a certain behavior of the borrow checker that I find rather odd: if I have a &'a mut &'b mut T where 'b: 'a, T: 'b, it seems as though I should be able to treat it as just a ...
0 votes
0 answers
128 views
Non-consuming mutable iterator over variable sized slices within vec [duplicate]
I have a struct (Lines) containing a vec of Coordinates. I'm trying to implement a non-consuming mutable iterator over non-overlapping sub-slices into this vector as &mut [Coordinate]. Each sub-...
2 votes
0 answers
111 views
Borrow checker yoga: Wrapping std::vec::Vec inside own type and adapt iter_mut [duplicate]
To implement a two dimensional vector type, I tried to wrap std::vec::Vec inside my own struct and implement a two dimensional mutable iterator. I know, that this is not possible in safe Rust due to ...
0 votes
0 answers
88 views
How to resolve lifetime iterator impl error [duplicate]
I have a data structure called 'FluidSim' which is a bunch of cells. Think of a 2d grid (x,y) which then holds a vector of data for each cell (grid location). I want to make an iterator which lets me ...
2 votes
0 answers
65 views
Why is this & lifetime different than &mut lifetime? [duplicate]
I'm creating a custom data structure in Rust and trying to implement iterators for it. Following the example of the built-in collections (LinkedList, Vec, etc), I've created an IntoIter struct that ...
3 votes
0 answers
40 views
Inferring lifetime fails due to conflicting requirements [duplicate]
I am trying to implement an Iterator that will go through a matrix line by line. struct Matrix { col_count: usize, backing: Vec<bool> } fn get_row_mut<'a>(&'a mut self, row: ...
1 vote
0 answers
37 views
Lifetime compiler error for implementing custom iterator. [duplicate]
I'm implementing Conway's Game of Life in various forms to reacquant myself with Rust. Currently I'm writing an implementation to use an iterator instead of nested for loops and got stuck on a ...