在其他模块中包含 main.rs

Include main.rs in other module

我是生锈新手。我知道,为了调用同一文件夹中的模块,我需要为其他文件夹 mod <module name>{ include!("path to module") } 编写 mod <module name>。我想在 extra.rs 中包含 main.rs,存在于同一个文件夹中,这样我就可以在 extra.rs 中对结构 feed 使用 Summary 特征。我收到错误 recursion limit reached while expanding the macro 'include'。 如何将 main.rs 包含在 extra.rs 中?有没有更好的方法来编写相同的代码?

错误

error: recursion limit reached while expanding the macro `include`
 --> src/extra.rs:3:5
  |
3 |     include!("main.rs");
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider adding a `#![recursion_limit="256"]` attribute to your crate

error: aborting due to previous error

error: could not compile `office_manager`.

main.rs

mod extra;

pub trait Summary {
    fn print_summry(&self) -> String;
}

pub struct Tweet {
    name: String,
    message: String
}

impl Summary for Tweet {
    fn print_summry(&self) -> String {
        format!("{}: {}",self.name,self.message)
    }
}

fn main() {

    let t = extra::Feed {
        name: String::from("Hanuman"),
        message: String::from("Jai sri Ram")
    };

    println!("{}",t.print_summry());

}

extra.rs

mod main {
    include!("main.rs");
}


pub struct Feed {
    pub name: String,
    pub message: String
}

impl Summary for Feed {
    fn print_summry(&self) -> String {
        format!("{}: {}",self.name,self.message)
    }
}

可以在 super 的帮助下访问父模块的元素。因此,在顶部添加 use super::*; 或使用 super::Summary 都有效。但最好使用 super::Summary,因为它不包含 main.rs.

的所有内容