Compile and run a Rust program using Cargo

InstructorPascal Precht

Share this video with your friends

Send Tweet

In this lesson we'll learn how to use the cargo run command to compile and run a Rust program.

J. Matthew
~ a year ago

The benefits of cargo run seem obvious, but I'm curious what is the difference between using cargo build from this lesson and rustc main.rs from lesson 2? I assume the latter is what the former is using under the hood but that it also generates other material useful for debugging and such?

J. Matthew
~ a year ago

Also worth noting: I tried out the official "Rust (rls)" extension for VS Code, and discovered that it also creates a folder under target, called rls, which is adjacent to the debug subfolder created by cargo build and cargo run. This is significant because it turns out that cargo clean doesn't delete the target folder indiscriminately, but rather the aforementioned debug subfolder, and the parent target only if there's nothing else in there. So, if you're using that extension, its content is preserved while the build is still deleted—very nice!

Pascal Prechtinstructor
~ a year ago

I assume the latter is what the former is using under the hood but that it also generates other material useful for debugging and such?

That is correct. cargo build lets you specify what target environment you want to compile to (e.g. development, production etc).

So, if you're using that extension, its content is preserved while the build is still deleted—very nice!

Ah, good to know! Thanks for bringing this up. I don't use VS Code so sorry this brought up confusion.