简体   繁体   English

如何在Rust中编译多文件包?

[英]How do I compile a multi-file crate in Rust?

I'm trying to figure out how to compile multi-file crates in Rust, but I keep getting a compile error. 我正在试图弄清楚如何在Rust中编译多文件包,但我不断收到编译错误。

I have the file I want to import into the crate thing.rs: 我有要导入到crate thing.rs的文件:

mod asdf {
    pub enum stuff {
        One,
        Two,
        Three
    }
}

And my crate file test.rc: 我的包文件test.rc:

mod thing;

use thing::asdf::*;

fn main(){

} 

When I run rust build test.rc I get: 当我运行Rust build test.rc时,我得到:

test.rc:3:0: 3:19 error: `use` and `extern mod` declarations must precede items
test.rc:3 use thing::asdf::*;
          ^~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

There's obviously something simple about how modules, crates and use work that I'm just not getting. 关于模块,板条箱和使用工作的方式显然有些简单,我只是没有得到。 My understanding was that mod something; 我的理解是mod某事; for files in the same directory or extern mod something; 对于同一目录或extern mod中的文件; for libraries on the library path caused the object file to be linked. 对于库路径上的库导致目标文件被链接。 Then use would allow you to import parts of the module into the current file, function or module. 然后使用将允许您将模块的部分导入当前文件,函数或模块。 This seems to work for stuff in the core library. 这似乎适用于核心库中的东西。

This is with version 0.6 of the rust compiler. 这是使用版本0.6的Rust编译器。

You just need to put the use at the top of the file: 您只需将use放在文件的顶部:

use thing::asdf::*;

mod thing;

fn main() {}

This looks very strange, but 这看起来很奇怪,但是

  1. It's what the error message says (anything that you can put at the top level that is not use or extern mod is an "item", including mod s), and 这是错误信息所说的内容(你可以放在最顶层的任何不useextern mod是“item”,包括mod ),以及
  2. It's how Rust name resolution works. 这是Rust名称解析的工作原理。 use is always relative to the top of the crate, and the whole crate is loaded before name resolution happens, so use thing::asdf::*; use总是相对于crate的顶部,并且在名称解析发生之前加载整个crate,所以use thing::asdf::*; makes rustc look for thing as a submodule of the crate (which it finds), and then asdf as a submodule of that, etc. 使rustc寻找作为箱子(它找到)的子模块的thing ,然后asdf作为其子模块,等等。

To illustrate this last point better (and demonstrate the two special names in use , super and self , which import directly from the parent and current module respectively): 为了说明这最后一点更好(并证明在这两个特殊的名称usesuperself ,直接从父分别当前模块导入):

// crate.rs

pub mod foo {
    // use bar::baz; // (an error, there is no bar at the top level)

    use foo::bar::baz; // (fine)
    // use self::bar::baz; // (also fine)

    pub mod bar {
        use super::qux; // equivalent to
        // use foo::qux; 

        pub mod baz {}
    }
    pub mod qux {}
}

fn main() {}

(Also, tangentially, the .rc file extension no longer has any special meaning to any Rust tools (including in 0.6), and is deprecated, eg all the .rc files in the compiler source tree were recently renamed to .rs .) (另外,切换, .rc文件扩展名不再对任何Rust工具(包括0.6)有任何特殊含义,并且不推荐使用,例如编译器源代码树中的所有.rc文件最近都重命名为.rs 。)

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

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