D
D
Daniil Demidko2016-08-03 12:59:59
C++ / C#
Daniil Demidko, 2016-08-03 12:59:59

How to compile multiple files together in Rust?

You can include the file as a module in the file:

//main.rs
mod other_file; // other_file.rs
use other_file::*;

This is somewhat similar to including a header in C++.
I'm not actually interested in this, but how to compile several files together
(Like in a project on pluses in any environment, or like in grids, when you can use a class declared in another from one file).
I didn't find anything in Rustbook.
I tried to put other files in /src and then run cargo run, but cargo ignores them (even if it's just text - no error occurs).
Is it even possible to do this in Rust?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DarkEld3r, 2016-08-11
@Daniro_San

Or I did not understand the question or there is something strange.
Rust "compiles together" in its entirety crate (and the Rustbook says so). If entities from one file could not be used in another, then how could such a language be used at all?
Well, "everything works for me." The project structure is as follows:

├── Cargo.lock
├── Cargo.toml
├── src
│   ├── first
│   │   └── mod.rs
│   ├── main.rs
│   └── second.rs

mod.rs:
pub fn foo() {
    println!("first::foo()");
}

second.rs:
pub fn bar() {
    println!("second::bar()");
}

main.rs:
mod first;
mod second;
use second::*;

fn main() {
    first::foo();
    bar();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question