1

I created a simple hello world program:

fn main() {
    println!("Hello, world");
}

When compiling the code using rustc vs cargo build, the cargo command appears slower. It takes 1.6s for cargo build vs 1s for rustc. See the timestamps on the right in the screenshot.

Why is this? Why should I still use cargo?

5
  • 8
    Can't you type out what's in your barely readable image?
    – Rob
    Jun 11, 2017 at 12:21
  • 7
    cargo is not a compiler, it's a package manager. It runs rustc and does some extra work (e.g. resolves dependencies), so it can't be faster than bare rustc. Jun 11, 2017 at 12:22
  • @Rob I added the time taken for both. Let me know if you need anything else. Jun 11, 2017 at 12:24
  • @Pavel Strakhov Thanks. This is what I was looking for Jun 11, 2017 at 12:24
  • 2
    That's like asking "why sould I use make while gcc is faster"...
    – Boiethios
    Jun 12, 2017 at 7:19

1 Answer 1

27

As Pavel Strakhov said

Cargo is not a compiler, it's a package manager. It runs rustc and does some extra work (e.g. resolves dependencies), so it can't be faster than bare rustc.

You can see this for yourself by running cargo build --verbose, which outputs the rustc command that cargo runs:

$ cargo build --verbose
   Compiling hw v0.1.0 (file:///private/tmp/hw)
     Running `rustc --crate-name hw src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=3c693c67d55ff970 -C extra-filename=-3c693c67d55ff970 --out-dir /private/tmp/hw/target/debug/deps -L dependency=/private/tmp/hw/target/debug/deps`
    Finished dev [unoptimized + debuginfo] target(s) in 0.30 secs

Why should I still use cargo

The output above shows one reason: Look at all those arguments passed to rustc. Do you know what each of them does? Do you want to know? Cargo abstracts some of the details away, allowing you to concentrate on the code.

Cargo also does so much more than just invoking the compiler. The biggest benefit to most people is that it manages your dependencies based on versions and allows publishing your own crates as well. It also allows for build scripts which run before your main compilation. It has easy ways of running your tests and examples.

More immediately useful, Cargo performs a check to see if you should rebuild at all:

$ time rustc src/main.rs
0:00.21
$ time rustc src/main.rs
0:00.22

$ time cargo build
0:00.41
$ time cargo build
0:00.09                   # Much better!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.