O
O
Onlyy2016-10-27 19:53:44
Rust
Onlyy, 2016-10-27 19:53:44

How to include external files in Rust?

How to include an external file in Rust? Registered in Cargo.toml:
[bin]
name="name"
path="src/name.rs"
After in the main.rs file:
use name::*;
But Cargo gives an error, what should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Orlov, 2016-10-28
@Onlyy

Through cargo, external libraries are connected - crates. And you probably need modules. To do this, the files must follow a standard hierarchy. For example, like this:
There are two files in the src directory: main.rs and foo.rs
--- main.rs ---
mod foo;
use foo::*;
fn main() {
boo();
}
--- foo.rs ---
pub fn boo() {
println!("boo()");
}
If submodules are needed, then instead of foo.rs we create the foo directory, put mod.rs in it, which already contains the foo module code, and at the same time the necessary submodules are declared ( How to access one file from another in Rust? ).
Details can be found here: https://doc.rust-lang.org/book/crates-and-modules.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question