In patching a crate, I took it on myself to hide the internal iterator type, but the author says that publishing the type is a feature, and that best practice is to publish an explicit wrapper structure for every iterator exposed in the public API. Apparently, the Rust standard library does this for all of its iterators.
Why do this? More concretely, if implementing a type that's compatible with std::env::Args, what are the pros and cons of using...
// implement iterator compatible with std::env::Args
pub struct Args { // public
// pub(crate) ...
}
impl Iterator for Args {
// ...
}
pub fn args() -> Args {
// ...
// return Args
}
vs
// implement iterator compatible with std::env::Args
pub(crate) struct Args { // hidden (outside of crate)
// pub(crate) ...
}
impl Iterator for Args {
// ...
}
pub fn args() -> impl Iterator<Item = String> {
// ...
// return Args
}