My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How do you manage your OO code in Rust?

Marco Alka's photo
Marco Alka
·Oct 26, 2016

I have been running into bigger chunks of Rust in my project lately, so I started to think about how to manage files in Rust.

One thing which came to my mind is the way I manage my Node.JS modules. So I tried reproducing the structure with.. minor issues; but I made it work! How do you manage your Rust modules?

Btw.: Sample of the mentioned structure in Rust:

// /src/main.rs

// Three lines for import is still something I have to sort out...
mod foo;
use foo::_struct::Foo;
use foo::*;

fn main() {
    let f = Foo{ my_var: true, };
    f.say_hello();
}
// /src/foo/mod.rs

// Also not too pretty; so much boilerplate for a little source code distribution across files
pub mod _struct;
pub mod say_hello;
pub use self::say_hello::SayHello;
// /src/foo/_struct.rs

/**
 * Struct with all the import fields goes here!
 * I will probably have to add all the methods as comments here to have one single interface...
 */
pub struct Foo {
    pub my_var: bool,

    /**
     * Write a friendly "Hello" to the console
     *
     * fn say_hello(&self);
     */
}
// /src/foo/say_hello.rs

use super::_struct::Foo;

pub trait SayHello {
    fn say_hello(&self);
}

impl SayHello for Foo {
    fn say_hello(&self) {
        println!("Hellow World <3");
    }
}