I have the following code:
pub struct Emails (
Vec<Email>
);
impl Emails {
pub fn print(&self) {
println!(" Inbox");
println!("=================\n");
self.0.iter()
.for_each(|email| {
println!("{:#?}\n", email);
});
}
pub fn filter(&self /*, some_predicate: implement later */, show_amount: Option<usize>) /* -> Need Maybe a New Type */ {
let filtered_emails = self.0.iter()
.filter(...)
.collect::<Vec<&Email>>();
// Problem is print method only works for Emails, which is a Vec<Email>
// I tried creating a new type for Vec<&Email> called EmailsSlice
}
}
This means I write redundant code:
pub struct Emails (
Vec<Email>,
);
impl Emails {
pub fn print(&self) {
EmailsSlice (
self.0.iter().collect()
).print()
}
}
pub struct EmailsSlice<'emails> (
Vec<&'emails Email>,
);
impl<'emails> EmailsSlice<'emails> {
pub fn print(&self) {
println!(" Inbox");
println!("=================\n");
self.0.iter()
.for_each(|email| {
println!("{:#?}\n", email);
});
}
}
I would like to use it like this:
match arguments.first() {
Some(argument) => {
let show_amount = argument.parse::<usize>().ok();
if let Some(filter) = arguments.get(1) {
user.inbox.filter(filter, show_amount)
.print();
}
},
None => {
user.inbox.print();
},
}