Rustc vs Cargo which is better to build

Hi guys, I recently learnt that I can create and build project using cargo.
Before now, i have been using rustc.
Which is better to use?

1 Like

I'd highly recommend cargo. It is the higher level tool for managing your rust projects.

rustc and it's flags are available if you're doing something particularly unusual but mostly isn't needed directly.

To get started you should know cargo new and cargo run. Or cargo new --lib and cargo test for a library.

There are lots of useful features in cargo and it is well introduced in the book: Hello, Cargo! - The Rust Programming Language

When you want to learn more it has its own chapter of the book: More about Cargo and Crates.io - The Rust Programming Language

5 Likes

You might note that cargo uses rustc. It is not a compiler by itself. Instead, it could be seen as a convenience wrapper, providing shortcuts to the commands used in almost every project. Then, one obvious (but extremely rare) case when you'd prefer rustc over cargo is if you need something which is easier to achieve by manually instructing the compiler then by describing your goal to cargo.

5 Likes

If you ever do want/need to call rustc, you can do that through Cargo too. That will automatically fill in the arguments that are required to build your dependencies.

Also, if you're curious, try running cargo build --verbose - it should show you the rustc commands that are being run under the hood.

2 Likes

A bit off topic but whilst we are here... Does anyone know how to get options through to the code generator from cargo and/or rustc?

For example when building for the ARM on the Raspberry Pi the options "-march=native" and "-mtune=native" can speed things up by 10% to 20% when compiling C with gcc or clang.

@ZiCog, are you looking for the RUSTFLAGS environment variable? Otherwise you may want to play around with the .cargo/config file for more control.

2 Likes

Thanks guys for the quick response.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.