按照本指南,我创建了一个货运项目
SRC/main.rs
fn main() {
hello::print_hello();
}
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
我运行使用
cargo build && cargo run
Run Code Online (Sandbox Code Playgroud)
它编译没有错误.现在我正在尝试将主模块分成两部分,但无法弄清楚如何从另一个文件中包含一个模块.
我的项目树看起来像这样
??? src
??? hello.rs
??? main.rs
Run Code Online (Sandbox Code Playgroud)
和文件的内容:
SRC/main.rs
use hello;
fn main() {
hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)
SRC/hello.rs
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译它src/main.rs
,我得到
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
Run Code Online (Sandbox Code Playgroud)
我试图遵循编译器的建议并修改main.rs
#![feature(globs)]
extern crate hello;
use hello::*;
fn main() {
hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)
但这仍然没有多大帮助,现在我明白了
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Run Code Online (Sandbox Code Playgroud)
有一个简单的例子,说明如何将当前项目中的一个模块包含到项目的主文件中?
另外,我每晚运行Rust 0.13.0和每晚0.0.1的货物.
Ren*_*non 180
您不需要mod hello
在您的hello.rs
文件上.任何文件的代码,但crate根(main.rs
对于可执行文件,lib.rs
对于库)在模块上自动命名.
要包含您的代码,hello.rs
请main.rs
使用mod hello;
.它会扩展到打开的代码hello.rs
(与之前完全一样).您的文件结构保持不变,您的代码需要稍微更改:
main.rs:
mod hello;
fn main() {
hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)
hello.rs:
pub fn print_hello() {
println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
小智 25
我真的很喜欢园丁的回应。我一直在使用我的模块声明的建议。如果有技术问题,请有人插话。
./src
??? main.rs
??? other_utils
? ??? other_thing.rs
??? utils
??? thing.rs
Run Code Online (Sandbox Code Playgroud)
主文件
#[path = "utils/thing.rs"] mod thing;
#[path = "other_utils/other_thing.rs"] mod other_thing;
fn main() {
thing::foo();
other_thing::bar();
}
Run Code Online (Sandbox Code Playgroud)
实用程序/东西.rs
pub fn foo() {
println!("foo");
}
Run Code Online (Sandbox Code Playgroud)
other_utils/other_thing.rs
#[path = "../utils/thing.rs"] mod thing;
pub fn bar() {
println!("bar");
thing::foo();
}
Run Code Online (Sandbox Code Playgroud)
小智 11
您需要mod.rs
文件夹中的文件.Rust by Example更好地解释了它.
$ tree .
.
|-- my
| |-- inaccessible.rs
| |-- mod.rs
| |-- nested.rs
`-- split.rs
Run Code Online (Sandbox Code Playgroud)
main.rs
mod my;
fn main() {
my::function();
}
Run Code Online (Sandbox Code Playgroud)
mod.rs
pub mod nested; //if you need to include other modules
pub fn function() {
println!("called `my::function()`");
}
Run Code Online (Sandbox Code Playgroud)
kei*_*fly 11
在非main.rs(或lib.rs)文件中,如果您想包含同一目录中的文件,则下面的代码可以工作。关键是使用“super::
包含”一词。(这就是我在不使用的情况下重写rodo的答案的方法path
方式。)
目录树:
\nsrc\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my.rs\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 a.rs\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 b.rs\n
Run Code Online (Sandbox Code Playgroud)\n将a.rs包含在b.rs中:
\npub fn function() {\n println!("src/my/a.rs/function()");\n}\n
Run Code Online (Sandbox Code Playgroud)\nuse super::b::function;\n\nfn f2() {\n function();\n}\n
Run Code Online (Sandbox Code Playgroud)\nmod a;\nmod b;\n
Run Code Online (Sandbox Code Playgroud)\nmod my;\n
Run Code Online (Sandbox Code Playgroud)\n
截至 2022 年
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 scripts\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 func.rs\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scripts.rs\n
Run Code Online (Sandbox Code Playgroud)\nfunc.rs
如果我想从文件(位于文件夹内)调用函数scripts
,我在根目录中创建了一个所谓的“链接”文件,与文件夹的名称相同(不需要链接文件名和文件夹名称相同)。
文件script/func.rs的内容:
\npub fn sayHello(){\n println!("Hello, World!");\n}\n
Run Code Online (Sandbox Code Playgroud)\n在script.rs文件中我有:
\npub(crate) mod func;
然后在我的main.rs
文件中我调用了该sayHello()
函数,如下所示:
mod scripts;\nfn main() {\n scripts::func::sayHello();\n}\n
Run Code Online (Sandbox Code Playgroud)\n
这个问题有两种情况,到目前为止我看到的答案只涵盖一种情况:当你有 a main.rs
、 anda.rs
和 a b.rs
,都在同一个目录中,但你想使用 froma.rs
或b.rs
in的函数main.rs
,但是我还没有看到有人介绍过相反的情况,所以我就在这里做一下。
给定main.rs
、a.rs
、 和b.rs
全部位于同一目录中,并且您希望在 A 中使用 B 中的函数,或在 B 中使用 A 中的函数:
main.rs
mod a;
mod b;
use crate::a::*;
use crate::b::*;
// To call a function from a.rs, do:
a::my_function_from_a();
Run Code Online (Sandbox Code Playgroud)
a.rs
use crate::b;
// To call a function from b.rs, do:
super::b::my_function_from_b();
Run Code Online (Sandbox Code Playgroud)
关键字super
ina.rs
就是神奇发生的地方。令人烦恼的是,该行为与 Ruby 版本不同super
。
如果你不使用 super 关键字,你会得到可怕的:[E0433] use of undeclared crate or module
。
归档时间: |
|
查看次数: |
53363 次 |
最近记录: |