相关疑难解决方法(0)

使用 mod 关键字访问 Rust 中的子目录

我有文件结构: engine 和 utils 文件夹在同一级别。引擎包含世界文件夹(其中包含 mod.rs 和 map.rs)和 mod.rs。Utils 包含 mod.rs 和 reader.rs。

我正在尝试从阅读器访问地图,但无法使“使用”语句起作用。

实用程序/mod.rs

pub mod reader;
Run Code Online (Sandbox Code Playgroud)

引擎/mod.rs

pub mod world;
Run Code Online (Sandbox Code Playgroud)

世界/mod.rs

pub mod map;

pub struct World;
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

pub mod reader;
Run Code Online (Sandbox Code Playgroud)

你不能在子目录中声明模块,还是我错过了一些愚蠢的东西?我一直在搞乱 use 语句大约半个小时,但无法让它工作。

rust

8
推荐指数
0
解决办法
4367
查看次数

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

我正在试图弄清楚如何在Rust中编译多文件包,但我不断收到编译错误.

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

mod asdf {
    pub enum stuff {
        One,
        Two,
        Three
    }
}
Run Code Online (Sandbox Code Playgroud)

我的包文件test.rc:

mod thing;

use thing::asdf::*;

fn main(){

} 
Run Code Online (Sandbox Code Playgroud)

当我运行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
Run Code Online (Sandbox Code Playgroud)

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

这是使用版本0.6的Rust编译器.

rust

6
推荐指数
1
解决办法
3524
查看次数

如何在 main.rs 中导入函数

这可能是一个愚蠢的问题,但我似乎无法解决这个问题。

\n

我有这样的文件结构:

\n
\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 another.rs\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_file.rs\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.rs\n
Run Code Online (Sandbox Code Playgroud)\n

在 中some_file.rs,我想调用 中的函数main.rs。所以,我尝试在some_file.rs

\n
use crate::main\n\n\nfn some_func() {\n   // other code\n   \n   main::another_func_in_main();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但编译器会抛出错误:

\n
use of an undeclared crate or module `main`\n
Run Code Online (Sandbox Code Playgroud)\n

我该如何解决这个问题?

\n

import rust rust-crates

4
推荐指数
1
解决办法
2106
查看次数

如何从tests目录中的文件访问src目录中的文件?

我有一个项目布局,如下所示:

src/
    int_rle.rs
    lib.rs
tests/
    test_int_rle.rs
Run Code Online (Sandbox Code Playgroud)

该项目编译cargo build,但我无法运行测试cargo test.我收到了错误

error[E0432]: unresolved import `int_rle`. There is no `int_rle` in the crate root
 --> tests/test_int_rle.rs:1:5
  |
1 | use int_rle;
  |     ^^^^^^^

error[E0433]: failed to resolve. Use of undeclared type or module `int_rle`
 --> tests/test_int_rle.rs:7:9
  |
7 |         int_rle::IntRle { values: vec![1, 2, 3] }
  |         ^^^^^^^^^^^^^^^ Use of undeclared type or module `int_rle`

error: aborting due to 2 previous errors

error: Could not compile `minimal_example_test_directories`.
Run Code Online (Sandbox Code Playgroud)

我的代码:

// …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo

1
推荐指数
1
解决办法
862
查看次数

标签 统计

rust ×4

import ×1

rust-cargo ×1

rust-crates ×1