I'm working on porting some code I wrote for my thesis to Rust. Something I need to get right and fast is an enumeration over all non-negative integer-valued vectors `x`

satisfying `dot_product(a,x)=k`

for positive integer `k`

and positive-integer-valued vector `a`

.

Here is my first attempt in Rust, with 3 "pain points" commented:

```
// The goal is to create a data structure that iterates over non-negative, integer-valued
// points on the plane a1*x1+a2*x2+...=k.
// BIG ASSUMPTION: a1 divides k
struct NonnegativeLattice {
a: Vec<u64>,
x: Vec<u64>,
init: bool,
}
impl NonnegativeLattice {
fn new(a: Vec<u64>, k: u64) -> NonnegativeLattice {
let mut x = vec![0; a.len()];
x[0] = k / a[0];
NonnegativeLattice { a: a, x: x, init: true, }
}
}
impl Iterator for NonnegativeLattice {
type Item = Vec<u64>;
fn next(&mut self) -> Option<Self::Item> {
// Pain point #1: awkward inclusion of lower bound, though how else to do this?
if self.init {
self.init = false;
Some(self.x.clone())
}
else {
let mut success = false;
// Pain point #2: this is glorified C -- what is the idiomatic way to do this?
// I hate the thought of all the bounds checks slowing this down.
// * the "algorithm" works by treating x[0] as a slush fund,
// adding/removing mass as necessary to preserve the dot product
for i in 1..self.x.len() {
if self.x[0] >= self.a[i] {
self.x[0] -= self.a[i];
self.x[i] += 1;
success = true;
break;
}
self.x[0] += self.x[i] * self.a[i];
self.x[i] = 0;
}
if success {
// Pain point #3: it feels weird to clone a vector for each iteration...
// I can promise that the returned value isn't modified and doesn't live past each
// iteration -- would a lifetimed reference work here instead? I couldn't figure it out
Some(self.x.clone())
}
else { // equivalent: all entries in x are now 0
None
}
}
}
}
fn main() {
for c in NonnegativeLattice::new(vec![1,2,3], 10) {
println!("{:?}", c);
}
}
```

Any advice would be much appreciated. The code works, but now that I'm not working on a time-crunch I want to do things the Right Way. Thanks!