DEV Community

John Behan
John Behan

Posted on • Originally published at boxitoff.com

Managing Rust binary files and Git when using rustc

What is rustc?

rustc is the compiler for Rust. When we use cargo to compile our source files it is actually calling rustc to do all the hard work.

Don't believe me? :D Use this command to see the full output from a call to cargo.

cargo build --verbose

rustc outputs the compiled binary file to the same directory as it is called in.

rustc hello.rs will create the binary file hello in the same directory as our source file hello.rs.

Where does Git come in to this?

All good. But if we are using Git we don't want to commit these binary files to our repository. "Let's just use a .gitignore file" I hear you cry. Perfect but we can only git ignore files and directories by name not by type or anything else.

A little trick we can use is to create a folder called bin and git ignore that. Then use the following when compiling with rustc -

rustc --out-dir bin hello

Now our source files and binary files are kept apart and our binaries never make it into the Git repository.

Top comments (2)

Collapse
 
ghost profile image
Ghost

but doesn't cargo deal with all that?, it makes a folder target/ with all the build "residue" and even makes a .gitignore with target/ on it. Whith other platforms sometimes I avoid those "scafoldings" but in the case of Rust Cargo doesn't add anything too fancy or complex. Is there some reason I don't see to not use cargo?

Collapse
 
jjmax75 profile image
John Behan

Yes, cargo takes care of this.

This post is for using rustc directly. I don't think there is any reason not to use cargo