The following code doesn't pass the borrow checker, for Label-A uses a value that consumed by Label-B, but the code is actually safe: the Label-A is guarded with processed
which is only set if Label-B is run.
How can I tell the compiler the dependency, or if I cannot, what's the idiom to solve this issue?
(Making X
Copy
/Clone
is not acceptable, nor making consume
taking a reference, neither Rc<X>
is appealing (the data structure is already quite complicate))
struct X(i32);
fn consume1(_x: X) {
()
}
fn consume2(_x: X) {
()
}
fn predicate(_x: &X) -> bool {
true
}
pub fn main() {
let xs = vec![X(1), X(2)];
for x in xs {
let mut processed = false;
// for _ in _ {
if predicate(&x) {
consume1(x); // Label-B
processed = true;
}
// } end for
// this for loop here is just to show that the real code
// is more complicated, the consume1() is actually called
// (somehow) inside this inner loop
// some more code
if !processed {
consume2(x); // Label-A
}
}
}