Reputation: 39
I wanna implement the feature that allow user to vote and change account value.
Now I get the error below.
cannot move out of dereference of `anchor_lang::prelude::Account<'_, BaseAccount>`
move occurs because value has type `Vec<ItemStruct>`, which does not implement the `Copy` trait
How can I change account value with iterator? Here is the part of my code.
pub fn vote(ctx: Context<Vote>, gif_id: u64) -> Result<()> {
let base_account = &mut ctx.accounts.base_account;
let user = &mut ctx.accounts.user;
let gif_iter = base_account.gif_list.into_iter(); // error occur here
match gif_iter.find(|item| item.id == gif_id) {
Some(gif) => gif.votes += 1,
_ => (),
}
Ok(())
}
Upvotes: 2
Views: 609
Reputation: 39
I solve this issue myself. I fixed function like below.
pub fn vote(ctx: Context<Vote>, gif_id: u16) -> Result<()> {
let base_account = &mut ctx.accounts.base_account;
let gif_iter = &mut base_account.gif_list.iter_mut();
gif_iter
.find(|item| item.id == gif_id)
.map(|found| found.votes += 1);
Ok(())
}
into_iter() function move ownership of account to new variable. So I got the error. I replace it with iter_mut() function. And also replacing match with map.
Upvotes: 1