Sorry for not making it clear. I have re-written the code and let me know how to achieve this:
mod my_module {
pub struct Foo {
id: usize,
}
impl Foo {
pub fn new(id: usize) -> Self {
// I shouldn't pass the Collection struct, as the user doesn't know about Collection.
let foo_obj = Self { id };
// I need to some how append foo_obj to Collection struct collection.foos.push(foo_obj).
foo_obj
}
}
// Returns number of total foos from the Collection
pub fn total_foos() ->usize{
// return the total foos
}
// Collects Foos
struct Collection {
foos: Vec<Foo>,
}
impl Collection {
fn len(&self) -> usize {
self.foos.len()
}
}
impl Default for Collection {
fn default() -> Self {
Self { foos: Vec::new() }
}
}
}
fn main() {
use my_module::{Foo,total_foos};
let foo_value = Foo::new(5); // As a user of my_module, I need not care about Collection
let foo1_value=Foo::new(3);
println!("Total foos are {}",total_foos()) // Should tell there are two foos
}