On 11/13/2013 02:06 PM, Huon Wilson wrote:
On 13/11/13 23:55, spir wrote:
Hello,
I have an error "cannot assign to immutable field". [...]
You need to use the .mut_iter() method, [T].iter() yields &T references, while
the former yields &mut T references (and can only be used on vectors that have
mutable contents, like ~[T] in a mutable slot, or &mut [T] or @mut [T]).
http://static.rust-lang.org/doc/master/std/vec/trait.MutableVector.html#tymethod.mut_iter
Right, that's it, i guess.
PS: tested: works fine!
(Unfortunately the docs are not good with respect to built-in types. Methods can
only be implemented on built-ins via traits, and the indication that the
built-ins implement a trait is only shown on the page for the trait itself.)
Actually, the vec module's doc tells which traits are implemented
[http://static.rust-lang.org/doc/0.8/std/vec/index.html]. However, it's just too
much at once for newcomers, in my opinion. There may be a subset of simple, base
functionality for vectors (but as someone said, the vec module needs
reorganisation).
I take the opportunity to ask whether there's an (i, item) iterator for arrays.
You can use the .enumerate() method on iterators, so
for (i, cell) in self.mut_iter().enumerate() {
...
}
http://static.rust-lang.org/doc/master/std/iter/trait.Iterator.html#method.enumerate
Right what I needed!
PS: tested: works fine!
(Also, you will possibly meet with mutable borrow complaints (I'm not 100% sure
of this), which you can address by destructuring self, so that the compiler
knows all the borrows are disjoint:
let StructName { lists: ref mut lists, cells: ref mut cells, n_lists: ref
mut n_lists } = *self;
and then use `lists`, `cells` and `n_lists` (they are just &mut references
pointing to the corresponding fields of self).)
Huon
Haven't met this issue yet, but I keep the pattern under the pillow for later
usage.
Thank you very much, Huon, for all this clear information.
Denis
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev