[英]Rust Reference Mods
I have a set of files in a workspace which I want to reference from another directory, but I cannot find the syntax I need to use.我在工作区中有一组文件,我想从另一个目录中引用这些文件,但是我找不到需要使用的语法。
The example is:例子是:
Primary
|_______ First
| |_____ src
| |______ Cool_thing.rs
|_______ Second
|_____ src
|______ main.rs
I want to reference a struct in Cool_things
from main.rs我想从 main.rs 引用Cool_things
一个结构
In Primary's Cargo.toml I put:在 Primary 的 Cargo.toml 中,我输入了:
[workspace]
members= ["First","Second", "Third"]
[dependencies.Second]
First= { path = "First" }
In the Cargo.toml in the Second
directory I have在Second
目录中的 Cargo.toml 我有
[dependencies]
First = { path = "../First" }
I have been trying things in my main.rs like:我一直在我的 main.rs 中尝试一些东西,比如:
extern crate First;
use First::Cool_things::Cool_things;
No variation of use
or mod
seems to allow me to reference the struct or impl or functions in mod Cool_things
. use
或mod
任何变化似乎都不允许我引用 mod Cool_things
的结构或 impl 或函数。
The problem was it needed a "lib.rs" in the First src directory.问题是它在第一个 src 目录中需要一个“lib.rs”。 Basically, the documentation for rust seems to be a bit remiss in telling people if you want to use local-dependancies then you have to have a lib.rs file which lists all the mods you are planning to access.基本上,rust 的文档似乎有点疏忽告诉人们如果您想使用本地依赖项,那么您必须有一个 lib.rs 文件,其中列出了您计划访问的所有 mod。 So the creation of a lib.rs in the First src directory, and listing "Mod Cool_things" would allow the other package to reference the mod files in that directory.因此,在 First src 目录中创建 lib.rs 并列出“Mod Cool_things”将允许其他包引用该目录中的 mod 文件。
The cargo.toml files: Inserted in the question above had the correct paths and dependancies, the issue was all around the lack of a lib.rs file. cargo.toml 文件:在上面的问题中插入的路径和依赖正确,问题在于缺少 lib.rs 文件。
So if you are planning a multiple binary program, you'll need to remember to have a lib.rs file whenever you're breaking up your code and then accessing modules from other parts of the workspace.因此,如果您正在计划一个多二进制程序,您需要记住在分解代码然后从工作区的其他部分访问模块时有一个 lib.rs 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.