Включить main.rs в другой модуль - PullRequest
0 голосов
/ 16 апреля 2020

Я новичок в ржавчине. Я знаю, для вызова модуля в той же папке мне нужно написать mod <module name> для другой папки mod <module name>{ include!("path to module") }. Я хочу включить main.rs в extra.rs, присутствующий в той же папке, чтобы я мог использовать черту Summary для структуры feed в extra.rs. Я получаю ошибку 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)
    }
}

enter image description here

1 Ответ

3 голосов
/ 16 апреля 2020

Элементы родительского модуля доступны с помощью super. Поэтому добавление use super::*; сверху или использование super::Summary оба работают. Но лучше использовать super::Summary, поскольку оно не включает в себя все main.rs.

...