I'll throw in my 2c. It answers more than your original question, though
I find this more important in libraries than applications, but I think the guidelines should be the same in this case:
Using traits to create an extension methods
pub trait Entries {
fn fill_cache(&self, cache: &mut Cache);
}
impl Entries for [Entry] {
fn fill_cache(&self, cache: &mut Cache) {
for entry in self {
/* … */
}
}
}
I think this should be the exeption (and very well documented if you choose this mehtod). It should mainly be used when you're either extending another trait (as in the case of TryStreamExt) or when providing general extensions for a type like new sorting methods on a slice or array etc. The main reason is discoverability and readability since you'll have to import these traits specifically, and for anyone reading the code it's not obvious where the methods come from.
The usage example provided by @jbe misses the import of the trait which you absolutely need to make it work if you use it from an external library or from a different module in the same project. I would never have considered importing the Entries trait to fill a cache (or any trait at all for that matter)...
use entry::Entries;
let entries: Vec<Entry> = /* … */
entries.fill_cache(&mut cache);
Using associated methods
impl Entry {
pub fn fill_cache(entries: &[Entry], cache: &mut Cache) {
for entry in entries {
/* … */
}
}
}
This is more a matter of taste, but I would be wary of creating public associated methods that doesn't construct a new type. Private associated methods is a different thing, as it might make sense in many cases to put "helpers" there to improve readability and avoid duplication, but in most cases I would prefer a private/public free function in the same module instead since that seems to be far more common in the projects I've seen.
Using a free-standing function
cache::fill_cache(&entries, &mut Cache);
I see this a lot in Rust and combined with good module naming this tells you pretty much everything you need. Sometimes I have a private utils module if there are helper type methods that I use in the whole project. I would consider "best practice" to use Rust's type system for what it's worth as long as it makes sense, though:
Liberal use of types
struct Entry { /* ... */ };
struct Cache { /* ... */ };
struct Entries(Vec<Entry>);
// Or implement `IntoIterator` or `Iterator` for `Entries`
impl Entries {
pub fn as_slice(&self) -> &[Entry] { self.0.as_slice() }
}
impl Cache {
pub fn fill(&mut self, entries: &Entries) {
for entry in entries.as_slice() { /* ... */ }
}
}
let mut cache = Cache { /* ... */ };
cache.fill(&mut self, &entries);
Especially for libraries, using the new type idiom and wrapping even simple types like Vec<Entry> makes sure the user (which might be you in 2 years) only sees the methods needed for Entries. It also allows you to change from a Vec to something else to store the collection later on if you wish to change from a Vec to a BTreeMap for some reason and keep the change contained to just the Entries type.