What Is Cargo?
I’ve never liked Makefiles, so it’s a relief to discover that Rust has its own build tool and dependency manager. Cargo can build your program and it can even download any libraries/dependencies that your program needs, automatically.
Before getting started with Cargo, you might want to check your installation is up to date, by running rustup update
.
The rustup
tool will check for updates.
Building Programs with Cargo
We’ll be using the following Cargo commands to create, work with and build a program which I’ve arbitrarily called hello.
cargo new hello
cargo build
cargo run
cargo build —-release
cargo run —-release
Open a terminal in a appropriate directory and run cargo new hello
.
This will create a directory called hello. In there we can find a Cargo.toml config file and a src subdirectory. In the src subdirectory, Cargo has created a “hello world” program, in the file hello.rs.
It also initialises a git repository, which is handy if you use git.
To build the program, type cargo build
.
This create a target subdirectory, and in there a debug subdirectory, which contains a debug version of your program.
You can run it in the normal way, by entering ./target/debug/hello
, but you can more conveniently run it by executing cargo run
.
When you change any code in the src directory, cargo run
will automatically detect the changes and rebuild the program as necessary.
To build a release version you can execute cargo build —-release
, and you can run it by executing cargo run —-release
.
By itself, cargo run
will run the debug version by default.
There is a Eclipse style IDE for Rustcalled RustRover. I think it even allows you to use AI to write short bits of code: https://www.jetbrains.com/rust/