Hi,
I'm new to Rust, working on my first project, a crate for Bayesian linear regression. I have two main structs:
SuffStats
holds sufficient statistics. For given data, this is fixed throughout the modeling process.Fit
holds the inferred weights and some other information. For efficiency, this needs to mutate in place.
Originally I had these bundled together, but it seemed like it might be better to split them up like this. In some cases, it could be convenient to have "many readers" for a given SuffStats
, but "one writer" for a Fit
.
Fitting the model is done iteratively, currently by a method
impl Fit {
fn update(&mut self, stats: &SuffStats) -> Option<&Self> {
...
}
}
This works well, but it would be more convenient if there's a way to use Rust's Iterator
trait. I've tried a few ways of setting up a single struct to hold both a SuffStats
and a Fit
, but I haven't yet gotten things to work out. It's fine if I clone
, but that has all kinds of overhead I'd like to avoid. Without that, I keep getting into all kinds of lifetime problems, and haven't been able to make it work out yet.
Looking around for the right way to do this, I came across streaming iterators, which says
The iterator APIs in the Rust standard library do not allow elements to be yielded which borrow from the iterator itself. That means, for example, that the
std::io::Lines
iterator must allocate a newString
for each line rather than reusing an internal buffer. TheStreamingIterator
trait instead provides access to elements being iterated over only by reference rather than by value.
Does this seem like a better approach for my application? As a newbie I don't yet have a good sense of idiomatic Rust, so I'd appreciate any design advice.