小编War*_*pig的帖子

Rust 中的跨模块函数调用

我正在尝试从另一个模块调用属于某个模块的函数(用于代码分解、组织)。

这是我的箱子结构:

?  mod_test git:(master) ? tree
.
??? Cargo.lock
??? Cargo.toml
??? src
    ??? bin
    ?   ??? one.rs
    ??? lib
    ?   ??? two.rs
    ??? main.rs

3 directories, 5 files
Run Code Online (Sandbox Code Playgroud)

main我声明:

pub mod bin {
    pub mod one;
}
pub mod lib {
    pub mod two;
}
Run Code Online (Sandbox Code Playgroud)

所有这些文件只包含一个微不足道的 pub fn main() {println!("hello");}.

此时,一切正常

现在,是它可以调用lib::two::mainbin/one.rs

use crate::lib::two;, use super::lib::two;, 都没有use self::super::lib::two;添加到bin/one.rs工作中。 …

module code-organization rust rust-crates

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

标签 统计

code-organization ×1

module ×1

rust ×1

rust-crates ×1