Ask coding questions

← Back to all posts
Multiple files in Rust?
DynamicSquid (4630)

How can I have multiple files in Rust compile down to a single executable?

My project structure looks like this:

Cargo.toml
src/
  main.rs
  error.rs
  front.rs

My current Cargo.toml looks like this:

[package]
name = "mirror"
version = "0.0.1"
authors = ["DynamicSquid"]
edition = "2018"

[dependencies]

[lib]
name = "error"
path = "src/error.rs"

And it works fine with just the main and error files, but I can't seem to get it to work with 3 or more files.

I don't think I'm understanding Rust's Cargo system correctly.

I was thinking of using the include! macro, but I heard it was bad practice.

Any ideas?

Would you be willing to help me @Jakman?

Comments
hotnewtop
Coder100 (16983)

first off, you don't need to change the cargo.toml file, that's nasty.

Second off, have you seen the rust tutorial?

use <rust-file>;

done

DynamicSquid (4630)

@Coder100 i don't think so. that's only for crates. crates can be compiled into libraries. but I can't figure out how to have multiple libraries link together