У меня есть две структуры одного типа внутри большей структуры.Я выберу одну из структур и выполню обновления, используя тот же код.
Упрощенная версия:
#[derive(Debug)]
struct Counts {
value: u16,
count: u8,
}
#[derive(Debug)]
struct Partitioned {
even: Counts,
odd: Counts,
}
fn sort() -> Partitioned {
let mut even = Counts { value: 2, count: 0 };
let mut odd = Counts { value: 3, count: 0 };
for i in 1..30 {
let mut s = if i % 2 == 0 { even } else { odd };
s.count = s.count + 1;
// ... a lot of other code
}
Partitioned { even, odd }
}
fn main() {
println!("{:?}", sort());
}
Это не компилируется с жалобами на право собственности:
error[E0382]: use of moved value: `even`
--> src/main.rs:19:37
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ^^^^ value moved here in previous iteration of loop
|
= note: move occurs because `even` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `odd`
--> src/main.rs:19:51
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ^^^ value moved here in previous iteration of loop
|
= note: move occurs because `odd` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `even`
--> src/main.rs:24:19
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ---- value moved here
...
24 | Partitioned { even, odd }
| ^^^^ value used here after move
|
= note: move occurs because `even` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `odd`
--> src/main.rs:24:25
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| --- value moved here
...
24 | Partitioned { even, odd }
| ^^^ value used here after move
|
= note: move occurs because `odd` has type `Counts`, which does not implement the `Copy` trait
Что здесь происходит?Как я могу реализовать это, не копируя весь код обновления в оба блока if
?