Once you have created and executed your first Rust program, you need to get to know Cargo, the project management and packaging tool for Rust. You don't need to know much to start using Cargo, but it will make learning Rust so much easier.
If you installed the Rust tools using
rustup, you should already
have Cargo in your system. You can verify it by giving the command
cargo --version in the terminal:
% cargo --version cargo 1.54.0 (5ae8d74b3 2021-06-22)
TIP: It's a good idea to verify that your Rust tools are up to date with
Create a new Rust project with Cargo in a directory you like, and call it
% cargo new hello-rust Created binary (application) `hello-rust` package
Cargo creates a new directory named
hello-rust and populates it
with some important files. It also generates a placeholder source file
the main program. It is almost exactly the same as the first
you created earlier.
Change to the
hello-rust directory and run the program using Cargo, with
% cd hello-rust % cargo run Compiling hello-rust v0.1.0 (/Users/me/Projects/Rust/hello-rust) Finished dev [unoptimized + debuginfo] target(s) in 1.33s Running `target/debug/hello-rust` Hello, world!
Rust compiled and ran the program. If you do it again right away, Cargo detects that the program source code hasn't changed, and will not compile it again:
% cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/hello-rust` Hello, world!
Note that by default Cargo compiles the program with debug information,
and also runs the unoptimized debug version. You can find the executable that was
created in the
target/debug directory (it's called
hello-rust.exe depending on your
cargo run --release compiles and
executes a release version:
% cargo run --release Compiling hello-rust v0.1.0 (/Users/me/Projects/Rust/hello-rust) Finished release [optimized] target(s) in 0.24s Running `target/release/hello-rust` Hello, world!
That's about all you need to know about Cargo for the time being. You can read more about Cargo in The Cargo Book.