Shall I make "function inheritance"?

Now I have create 2 files: src/s1.rs and src/s2.rs;
s1.rs looks like below:

pub mod general {
    pub fn foo() {
        ...
    }
}

s2.rs looks like below:

pub mod general {
    // here i want to inheritate functon 'foo' from s1.rs
}

is it possible for that?

I'm not sure what you're trying to ask. If you want to export the function foo from s1::general in s2::general, use the pub use syntax.

In src/s2.rs:

pub mod general {
    pub use crate::s1::general::foo;
}

What you are trying to do doesn't seem to match the design pattern of inheritance or functional inheritance (from JS).

2 Likes

It warns "failed to resolve: could not find s1 in the crate root"

main.rs:

mod s2;

fn main() {

}

s2.rs

pub mod general {
    pub use crate::s1::general::foo;
}

s1.rs

pub mod general {
    pub fn foo() {
        dbg!("hello");
    }
}

It warns: "failed to resolve: could not find s1 in the crate root
could not find s1 in the crate root" on s2.rs

You have to declare any modules.

+mod s1;
 mod s2;
 fn main() {}
1 Like

thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.