简体   繁体   English

包含来自不是main.rs或lib.rs的另一个文件

[英]Including a file from another that is not main.rs nor lib.rs

(I'm a Rust beginner) I have three files : main.rs, board.rs and case.rs. (我是Rust的初学者)我有三个文件:main.rs,board.rs和case.rs。 I want to include case.rs into board.rs, and board.rs into main.rs, so the board uses the case, and we can access the board in main. 我想将case.rs包含在board.rs中,并将board.rs包含在main.rs中,以便开发板使用大小写,并且我们可以访问main中的开发板。

I've successfully added the board into main, but the way I did doesn't seem to work for the second part. 我已经成功地将主板添加到了主面板中,但是第二部分似乎没有用。

I've tried to encapsulate every file's content into "mod {}" but it doesn't change the problem. 我试图将每个文件的内容封装到“ mod {}”中,但这并没有改变问题。 Also I tried every combinations of "mod" and "use". 我也尝试了“ mod”和“ use”的每种组合。

Every file is in the folder src/, and I'd like them not to move from there if possible. 每个文件都在文件夹src /中,如果可能的话,我希望它们不要从那里移动。

main.rs main.rs

mod board;

fn main() {
    let mut b: Board = Board::new();
}

board.rs board.rs

mod case;

pub struct Board {
    board: [ Case; 9 ]
}

// There is also the impl part of course, let's keep it short

case.rs case.rs

pub enum Case { Empty, Full(Player) }

Using VSCode with the Rust plugin, the "case" word on the first line of the board.rs file is underlined red, and it says : 将VSCode与Rust插件一起使用时,board.rs文件第一行的“ case”字样用红色下划线标出,并表示:

"src/case.rs file not found for module case help: name the file either board\\case.rs or board\\case\\mod.rs inside the directory "src"" “找不到模块case帮助的src / case.rs文件:在目录“ src”中将文件命名为board \\ case.rs或board \\ case \\ mod.rs。”

Why doesn't it search in the current directory? 为什么不在当前目录中搜索?

Your files could look like as follows: 您的文件可能如下所示:

case.rs: case.rs:

#[derive(Clone, Copy, Debug)]
struct Player;

#[derive(Clone, Copy, Debug)]
pub enum Case {
    Empty,
    Full(Player)
}

board.rs: board.rs:

use crate::case::Case;

#[derive(Debug)]
pub struct Board {
    board: [ Case; 9 ]
}

impl Board {
    pub fn new() -> Self {
        Self { board: [Case::Empty; 9] }
    }
}

main.rs: main.rs:

mod case;
mod board;

use crate::board::Board;

fn main() {
    println!("{:?}", Board::new());
}

Basically you create a crate (a binary one, because of your main.rs ) and that crate can have modules. 基本上,您创建一个crate (由于main.rs而为二进制crate ),并且该crate可以具有模块。 A module can be a file, or it can be a folder as well (if it happens to have a mod.rs ). 模块可以是文件,也可以是文件夹(如果碰巧有mod.rs )。 (And for the sake of completeness, it could also be an inline module without direct relation to the file system.) (为了完整起见,它也可以是与文件系统没有直接关系的内联模块。)

Your mod statements (the ones which are indicating files and folders, not the ones which you use to create inline modules) should either be placed at the top level of your crate (eg in your main.rs or lib.rs ) or at the module levels (in your mod.rs files) depending on the desired structure. 您的mod语句(用于指示文件和文件夹的语句,而不是用于创建内联模块的语句)应该放在crate的顶层(例如,在main.rslib.rs )或模块级别(在您的mod.rs文件中),具体取决于所需的结构。

For further info on this, please read the The Rust Programming Language book's relevant chapter: Packages and Crates . 有关此的更多信息,请阅读《 Rust编程语言》一书的相关章节: 包和板条箱

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

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