Move structure to a separate file without splitting into a separate module?

Let's say I have this file hierarchy:

main.rs
protocol/
protocol/mod.rs
protocol/struct.rs

In struct.rs:

pub struct Struct {
    members: i8
}

impl Struct {
    pub fn new() -> Struct {
        Struct { 4 }
    }
}

How can I access it as:

mod protocol;
protocol::Struct::new();
// As opposed to:
// protocol::struct::Struct::new();

I tried various combinations pub useand mod, but admittedly, I blindly talk about things.

Is it possible to split the structure (and its impl) into a separate file without the need to create a new mod?

+4
source share
1 answer

Short answer: use pub use Typeat mod.rs. The following is a complete example:

My structure:

/src/main.rs
/src/protocol/
/src/protocol/mod.rs
/src/protocol/thing.rs

main.rs

mod protocol;

fn main() {
    let a = protocol::Thing::new();
    println!("Hello, {:?}", a);
}

protocol /mod.rs

pub use self::thing::Thing;
mod thing;

Protocol /thing.rs

#[derive(Show)]
pub struct Thing(i8);

impl Thing {
    pub fn new() -> Thing { Thing(4) }
}

, , . struct , . , , ^ _ ^.

, : , - , . , , .

+5

All Articles