简体   繁体   English

在其他模块中包含 main.rs

[英]Include main.rs in other module

I'm new to rust.我是 rust 的新手。 I know, for calling a module in same folder I need to write mod <module name> for other folder mod <module name>{ include!("path to module") } .我知道,为了调用同一文件夹中的模块,我需要为其他文件夹mod <module name>{ include!("path to module") }编写mod <module name> I want to include main.rs in extra.rs , present in same folder, so that I can use Summary trait for structure feed in extra.rs .我想将main.rs包含在extra.rs中,存在于同一文件夹中,这样我就可以在extra.rs中使用Summary trait 作为结构feed I get error recursion limit reached while expanding the macro 'include' . recursion limit reached while expanding the macro 'include' How I can include main.rs in extra.rs ?如何将main.rs包含在extra.rs中? Is there a better way to write the same code?有没有更好的方法来编写相同的代码?

error错误

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 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额外的.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)
    }
}

在此处输入图像描述

Elements of parent module can be accessed with help of super .可以在super的帮助下访问父模块的元素。 Therefore adding use super::*;因此添加use super::*; on top or using super::Summary both works.在顶部或使用super::Summary都有效。 But it is better to use super::Summary as it don't include everything of main.rs .但最好使用super::Summary因为它不包括main.rs的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM