如何在 Rust 中编译多文件 crate? [英] How do I compile a multi-file crate in Rust?

查看:68
本文介绍了如何在 Rust 中编译多文件 crate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在 Rust 中编译多文件 crate,但我一直收到编译错误.

I'm trying to figure out how to compile multi-file crates in Rust, but I keep getting a compile error.

我有我想导入到 crate thing.rs 的文件:

I have the file I want to import into the crate thing.rs:

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

还有我的箱子文件 test.rc:

And my crate file test.rc:

mod thing;

use thing::asdf::*;

fn main(){

} 

当我运行 rust build test.rc 时,我得到:

When I run rust build test.rc I get:

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

很明显,关于模块、板条箱和使用工作的一些简单的东西我没有得到.我的理解是 mod 的东西;对于同一目录中的文件或 extern mod 的东西;对于库路径上的库导致目标文件被链接.然后使用将允许您将模块的部分导入到当前文件、函数或模块中.这似乎适用于核心库中的内容.

There's obviously something simple about how modules, crates and use work that I'm just not getting. My understanding was that mod something; for files in the same directory or extern mod something; 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.

这是 0.6 版的 Rust 编译器.

This is with version 0.6 of the rust compiler.

推荐答案

你只需要把use放在文件的顶部:

You just need to put the use at the top of the file:

use thing::asdf::*;

mod thing;

fn main() {}

这看起来很奇怪,但是

  1. 这就是错误消息所说的(您可以放在顶层的任何不是 useextern mod 的内容都是项目",包括 mods) 和
  2. 这就是 Rust 名称解析的工作原理.use 总是相对于 crate 的顶部,并且在名称解析发生之前加载整个 crate,所以 use thing::asdf::*; 使 rustc 寻找thing 作为 crate(它找到的)的子模块,然后 asdf 作为它的子模块,等等.
  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 mods), and
  2. It's how Rust name resolution works. use is always relative to the top of the crate, and the whole crate is loaded before name resolution happens, so 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.

为了更好地说明最后一点(并演示 use 中的两个特殊名称,superself,它们直接从父级导入和当前模块):

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):

// 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() {}

(另外,.rc 文件扩展名对于任何 Rust 工具(包括在 0.6 中)不再具有任何特殊意义,并且已被弃用,例如所有 .rc编译器源代码树中的 code> 文件最近被重命名为 .rs.)

(Also, tangentially, the .rc file extension no longer has any special meaning to any Rust tools (including in 0.6), and is deprecated, e.g. all the .rc files in the compiler source tree were recently renamed to .rs.)

这篇关于如何在 Rust 中编译多文件 crate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆